ruby on rails - Accessing a HABTM join table in the view -
i working on creating shopping lists in app , struggling understand methods of using join table return desired attributes of inventory_item in views. want return these attributes of inventory_item in lists - :price, :vendor, :name, :details, :brand. have these relevant models:
what best practice working join table in view? can suggest simple console testing familiarize myself practice? in advance!
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
thanks @grantovich able join table set accordingly:
class listitemjoin < activerecord::migration def change create_table :inventory_items_shopping_lists, id: false |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
i newish ror, critiques or suggestions appreciated, thank you!
you can like
<ul> <% @shopping_list.inventory_items.each |item| %> <li><%= item.price %></li> <li><%= item.vendor %></li> <% end %> </ul>
and on. same thing @vendor.items
Comments
Post a Comment