我有一个关于跨越多个关联级别的问题,如果有人能帮我吗?
我有一个仓库,我正试着按产品的标签号来调出存货。到目前为止,我可以通过和使用标记模型中定义的方法,并轻松获取相关的产品名称属性:
#Warehouse.rb
has_many :tags, as: :location
has_many :products, through: :tags
#Tag.rb
belongs_to product #
#polymorphic
belongs_to :trackable, polymorphic: true
belongs_to :location, polymorphic: true
belongs_to :product
),我不断遇到问题;我想在迭代库存列表中定义类别,并可以选择按其排序/分组:
#Product.rb
has_one :primary_category_assignment
has_many :rfid_tags, as: :trackable
每个产品都被分配到一个类别-关联是仓库>标签>产品>类别分配>类别。我想深入到最后两个层次。
#CategoryAssignment.rb #this is a join table
belongs_to :product, required: true
belongs_to :category, required: true
#Category.rb
has_many :products, through: :category_assignment
下面是我用来设置此设置的一些附带代码:
#warehouse_controller.rb
def index
@tags = @warehouse.tags.all.joins(:ancestor_product)
end
#warehouses/index.html.erb
<p><% @tags.each do |tag| %>
tag: <%= tag.number %>,
location_id <%= tag.location_id %>,
<%= tag.product.name %>,
Purchased at $<%= tag.product.purchase_price %></p> #grabs an attribute from the product level
#Next level would reach through the category assignment join table (category_id) to grab the category, or at least order by the category assignment
以下架构供参考:
#schema
create_table "categories", force: :cascade do |t|
t.string "name", null: false
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "position"
end
add_index "categories", ["position"], name: "index_categories_on_position", using: :btree
create_table "category_assignments", force: :cascade do |t|
t.integer "product_id"
t.integer "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.boolean "primary", default: false
end
create_table "products", force: :cascade do |t|
t.string "name", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.float "purchase_price"
t.text "description"
t.datetime "purchase_date"
end
add_index "category_assignments", ["category_id"], name: "index_category_assignments_on_category_id", using: :btree
add_index "category_assignments", ["product_id"], name: "index_category_assignments_on_product_id", using: :btree
create_table "tags", force: :cascade do |t|
t.string "number", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "location_type"
t.integer "location_id"
t.string "trackable_type"
t.integer "trackable_id"
t.integer "product_id"
end
add_index "tags", ["product_id"], name: "index_rfid_tags_on_ancestor_product_id", using: :btree
add_index "tags", ["location_id"], name: "index_tags_on_location_id", using: :btree
add_index "tags", ["location_type"], name: "index_tags_on_location_type", using: :btree
add_index "tags", ["trackable_id"], name: "index_tags_on_trackable_id", using: :btree
add_index "tags", ["trackable_type"], name: "index_tags_on_trackable_type", using: :btree
add_index "tags", ["user_id"], name: "index_tags_on_user_id", using: :btree
create_table "warehouses", force: :cascade do |t|
t.string "name", null: false
end