Отношение «многие ко многим ко многим» с необходимостью в другой конкретной модели
У меня есть отношения многие ко многим междуСупермаркет,Товар а такжемарка сквозьПоставка- а такжепроисхождения-моделей. Я также хочу хранить информацию о том, какая конкретная комбинация продукта-бренда у меня есть в моем супермаркете. Я думал о другой модели (я назвал этоSpecific_Combination
где я буду хранить,:supermarket_id
:product_id
а также .:brand_id
class Supermarket < ActiveRecord::Base
has_many :supplies
has_many :products, :through => :supplies
end
class Supply < ActiveRecord::Base
belongs_to :product
belongs_to :supermarket
end
class Product < ActiveRecord::Base
has_many :supplies
has_many :supermarkets, :through => :supplies
has_many :origins
has_many :brands, :through => :origins
end
class Origin < ActiveRecord::Base
belongs_to :products
belongs_to :brands
end
class Brand < ActiveRecord::Base
has_many :origins
has_many :products, :through => :origins
end
А теперь класс, который я думал, я мог бы использовать для хранения определенной комбинации продукта-бренда
class Specific_Combination < ActiveRecord::Base
# to show which columns I would use:
attr_accessible :supermarket_id, :product_id, :brand_id
end
Это подходящий подход?Как мне смоделировать отношения с и из?Specific_Combination
Как я могу получить доступ (создать ...) предметы в?Specific_Combination
Как будет выглядеть лучший подход (нормализация)?редактировать
class Supply < ActiveRecord::Base
belongs_to :origin
belongs_to :supermarket
end
class Product < ActiveRecord::Base
has_many :origins
end
class Origin < ActiveRecord::Base
belongs_to :product
belongs_to :brands
end
class Brand < ActiveRecord::Base
has_many :origins
end
class Supermarket < ActiveRecord::Base
has_many :supplies
has_many :origins, :through => :supplies
# my attempt to create an array of names of supermarkets
def self.to_be_chosen
chosen_supermarket = Array.new
Supermarket.find_each do |supermarket|
chosen_supermarket < supermarket.name
end
return chosen_supermarket
end
end