Я работаю над созданием списков покупок в своем приложении и изо всех сил пытаюсь понять методы использования таблицы соединений для возврата желаемых атрибутов 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, поэтому любая критика или предложения приветствуются, спасибо!