アプリでショッピング リストの作成に取り組んでおり、結合テーブルを使用してビューで inventory_item の目的の属性を返す方法を理解するのに苦労しています。リスト内の inventory_item の属性 (:price、:vendor、:name、:details、:brand) を返したいと考えています。私はこれらの関連モデルを持っています:
ビューでこのような結合テーブルを操作するためのベスト プラクティスは何ですか? 練習に慣れるための簡単なコンソール テストを提案してもらえますか? 前もって感謝します!
class InventoryItem < ActiveRecord::Base
belongs_to :item, :foreign_key => :item_id
belongs_to :vendor
has_and_belongs_to_many :shopping_lists
end
class Item < ActiveRecord::Base
has_many :inventory_items
has_many :vendors, through: :inventory_items
end
class ShoppingList < ActiveRecord::Base
has_and_belongs_to_many :inventory_items
belongs_to :user
end
class Vendor < ActiveRecord::Base
has_many :inventory_items
has_many :items, through: :inventory_items
has_many :shopping_list_items
end
class User < ActiveRecord::Base
has_many :shopping_lists
end
@Grantovich のおかげで、それに応じて結合テーブルを設定することができました。
class ListItemJoin < ActiveRecord::Migration
def change
create_table :inventory_items_shopping_lists, id: false do |t|
t.references :inventory_item
t.references :shopping_list
end
add_index :inventory_items_shopping_lists, :inventory_item_id
add_index :inventory_items_shopping_lists, :shopping_list_id
add_index :inventory_items_shopping_lists, [:inventory_item_id, :shopping_list_id], unique: true, :name => 'my_index'
end
end
私はRoRに慣れていないので、批評や提案をお待ちしております。ありがとうございます!