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