0

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

c_ern
  • 151
  • 2
  • 11
  • I don't get the Origins table. A product has many brand origins? I don't even know what that means. A product belongs to a brand. I would change supply to Inventory and add a quantity field – Neil McGuigan Sep 16 '12 at 18:47
  • Maybe `Product` is not the best description for what I mean here. I probably should call it `Category`- like Cola can come from Coca Cola or PepsiCo... – c_ern Sep 17 '12 at 10:42

1 Answers1

0

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)
Marlin Pierce
  • 9,931
  • 4
  • 30
  • 52
  • Ah okay, I see: `SpecificCombination` is not necessary, I `could have `origin_id` in *Supply*. But how do I create a new entry in *Supply*? I do not mean a new column but a new database entry (`Supermarket.create(:name => 'Walmart').origin.create('')`)? – c_ern Sep 16 '12 at 14:36
  • 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) – Marlin Pierce Sep 16 '12 at 19:49
  • Great, that's the solution I was looking for. Sorry for not expressing it clearly in my previous question. **Bonus question:** How can I access the `Supply`-Model via Drop-Down-Menu like in [this question](http://stackoverflow.com/questions/12406583/drop-down-menu-for-many-to-many-relation-in-rails-using-nested-attributes?rq=1)? – c_ern Sep 17 '12 at 10:41
  • Typically, I have a choices class method in my ActiveRecord classes which return an array of [name, id] arrays. I can pass this to form.select to create a drop down box. I suggest a choices class method in the Supply class which returns an array of choices for the drop down box which is an array of [description, id] arrays to pass to the select method, and format description however you want to present the supermarket, origin combination. (Likewise for the Origin model if needed.) – Marlin Pierce Sep 17 '12 at 15:11
  • I'm sorry to bother you again... but I did not yet figure out how to access the variables from a choice-model-class. I think that should look something like: `def self.to_be_chosen chosen_supermarket = Array.new Supermarket.find_each do |supermarket| chosen_supermarket << supermarket.name end end` – c_ern Sep 19 '12 at 13:48
  • Newbie question: Should I move this part of the question to a whole new one as it wasn't the core part of this one in the first place? So you could earn more reputation? – c_ern Sep 19 '12 at 14:24
  • I'm not sure what you mean by a choice-model class. It might help to write that up as a new question. – Marlin Pierce Sep 19 '12 at 15:21
  • okay, forgot return... is this a suitable approach that i could use in supply? – c_ern Sep 19 '12 at 15:22
  • Sorry, now I get it. (StackOverflow does not preserve newlines in comments, so it's hard to reference code in comments.) I edited my answer to add a choice method. – Marlin Pierce Sep 19 '12 at 18:34
  • Thanks for being so patient! I think it would help if I shared the sample code on github in the future... FYI: I have asked a new question regarding the selection [here](http://stackoverflow.com/questions/12499753/form-to-create-entries-in-many-to-many-relationship). – c_ern Sep 19 '12 at 18:59