问题
I have a many-to-many relationship between Supermarket, Product and Brand through the Supply- and Origin-models.
I also want to store which specific Product-Brand-Combination I have in my supermarket.
I thought of another model (I called it Specific_Combination
where I would store :supermarket_id
, :product_id
and :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
And now the class I thought i could use to store a specific Product-Brand-combination
class Specific_Combination < ActiveRecord::Base
# to show which columns I would use:
attr_accessible :supermarket_id, :product_id, :brand_id
end
- Is this a suitable approach?
- How do I have to model the relationships to and from
Specific_Combination
? - How would I access (create...) the items in
Specific_Combination
? - How would a better approach (normalization) look like?
Edit
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
/Edit
回答1:
Here, I might have supply belong to origin instead of product.
Also, think about whether you need SpecificCombination. What operations are you going to do with it? Are you going to list all the SpecificCombinations in your database? Are you going to search to find a specific one? Are you going to create a new combination?
Then think if these operations can be done simply with the other classes?
class Supermarket < ActiveRecord::Base
has_many :supplies
has_many :origins, :through => :supplies
def self.choice
Supermarket.all.map { |supermarket| [ supermarket.name, supermarket.id ] }
end
end
end
class Supply < ActiveRecord::Base
belongs_to :origin
belongs_to :supermarket
end
class Origin < ActiveRecord::Base
belongs_to :supplies
belongs_to :brands
end
walmart = Supermarket.create(:name => "Walmart");
cornflakes = Product.create(:name => "Corn Flakes");
kellogs = Brand.create(:name => "Kellog's");
walmart.origins.create(:product_id => cornflakes, :brand_id = kellogs)
来源:https://stackoverflow.com/questions/12445675/many-to-many-to-many-relationship-with-need-for-another-specific-model