跨多个关联/联接表访问has_one对象

时间:2018-10-22 04:12:11

标签: ruby-on-rails ruby-on-rails-4 model-view-controller associations polymorphic-associations

我有一个关于跨多个协会级别的问题,是否有人可以帮助我?

我有一个仓库,我想按产品的标签号调出产品清单。到目前为止,我可以通过并使用Tag模型中定义的方法,并且足够容易地获取关联的产品名称属性:

#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

每个产品都分配到一个类别-关联是仓库>标签>产品> category_assignment>类别。我正尝试深入到最后两个级别。

#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

0 个答案:

没有答案