0

Maybe I am a little slow on the uptake today (I can't even phrase a proper caption), but I want to solve this issue:

I want to expand my model from my previous question.

Short recap:

A supermarket can hold many products and each product can be sold in many supermarkets. The association is build via the Supply-model.

Expansion:

Now I want to expand this model: Let's say I have a product Apple (the fruit). It can come in different cultivars: i.e. "Granny Smith", "Golden Delicious" and so on.

In Supermarket 1 I can buy

  • Apple -> "Granny Smith"
  • Apple -> "Golden Delicious"

In Supermarket 2 I can buy

  • Apple -> "Braeburn"
  • Apple -> "Golden Delicious"
  • Apple -> "McIntosh" (really, that's an apple cultivar).

Edit:

A more common issue is to have different companies that deliver the same product: Cornflakes can be supplied by Kellog's or General Mills or so on, where at the same time these companies produce many products:

Supermarket 1 sells:

  • Cornflakes -> Kellog's
  • Cornflakes -> General Mills

Supermarket 2 sells:

  • Cornflakes -> Kellog's
  • Rice Kriespies -> Kellog's

I think I need an additional model to connect brand (cultivar in the model above) and product (Cornflakes / Apple).

/Edit

What Models do I have to create additionally and how do I have to link them up? I suppose something like polymorphic associations, but I don't really have a clue...

Community
  • 1
  • 1
c_ern
  • 151
  • 2
  • 11

1 Answers1

0

You probably want to have the many-to-many relationship between the supermarket and the detailed product. The detailed product would have a foreign key to the general product.

Let's say you name the general product model genus (plural: genera). [Feel free to find a better name.]

class Genus
  has_many :products
end

class Brand
  has_many :products
end

class Product
  belongs_to :genus
  belongs_to :brand
  has_many :supplies
  has_many :supermarkets, :through => :supplies
end

class Supply
  belongs_to :product
  belongs_to :supermarket
end

class Supermarket
  has_many :supplies
  has_many :products, :through => :supplies
end


Product                   Genus
Granny Smith Apple        Apple
Golden Delicious Apple    Apple
Braeburn Apple            Apple
Macintosh Apple           Apple
Kellog's Cornflakes       Cornflakes
GM Cornflakes             Cornflakes
Kellog's Rice Krispies    Rice Krispies

You might also include a foreign key in Product to brand, to store Kellog's or General Mills.

Marlin Pierce
  • 9,931
  • 4
  • 30
  • 52
  • This model seems to solve the first issue - edited the question because the problem to solve is more complex – c_ern Sep 14 '12 at 18:14
  • Edited addressing second problem. – Marlin Pierce Sep 14 '12 at 18:33
  • I don't know if that solves my problem - maybe the apple-example was a little distractive. Perhaps I should say, the main problem is to connect **supermarket**, **product (or genus if you will)** and the **brand**... how exactly do i have to create my models? – c_ern Sep 14 '12 at 19:32
  • Just a quick note: I think I need an additional model (called `super_attribution` with `supermarket_id`, `product_id` and `brand_id`. but how they belong together (`belongs_to`, `has_many` and `has_many, :through`, I don't get it in my head... – c_ern Sep 14 '12 at 21:37
  • "the main problem is to connect supermarket, product (or genus if you will) and the brand" It seems to me that you should connect supermarket with the most detailed version of the product, connect product to a less detailed type, and connect product to the brand. I'll edit my answer to include the rest of the models. – Marlin Pierce Sep 15 '12 at 21:06
  • Okay, I will try this approach... But I thought about my problem and are now capable of describing it better, so I posted a new [question](http://stackoverflow.com/questions/12445675/many-to-many-to-many-relationship-with-need-for-another-specific-model). Thank you for your help, maybe you can help me with the new question. – c_ern Sep 16 '12 at 09:11
  • Good, because, either you have a problem that you are not expressing in your question, or you are not able to see how this solution works for you. Basically, you have a one-to-many relationship on your product side, and you are trying to relate it to supermarket. Another notation for one-to-many is master-detail. You seem to keep trying to relate supermarket to the master, and I am advising relating it to the detail. A good example is the brand. Brand is *not* related to supermarket. It is related to product, and only related to supermarket through the product. – Marlin Pierce Sep 16 '12 at 12:52