With the relationship you have set up below, you can only have one food and one sauce associated with any wine. This is why wine.foods will return nothing -- wine.food would work in the scenario you have set up. I'm sure you'd agree that doesn't properly model the desired relationship. Other than that, you are doing the mechanics of has_many :through correctly. To get the relationships you want, I would set up a three-way join table. That is, log each combination of wine, food and sauce as an individual record. It would look something like this:

create_table :pairings do |t|
 t.integer :food_id
 t.integer :wine_id
 t.integer :sauce_id
end

Then you can choose any of the pairs of Food, Wine, and Sauce to create a combination, or fill in all three. Your associations in the Wine class would look like this:

has_many :pairings
has_many :foods, :through => :pairings
has_many :sauces, :through => :pairings

Your other classes would look similar.  Does this help?

Sean

[EMAIL PROTECTED] wrote:
Hey guys,

I hope I am not offending anyone by posting slightly OT questions here,
but it is next to impossible to get an answer on the Rails forum
sometimes. This is related to an extension I am working on, so it isn't
completely OT.
I have a problem groking has_many through associations. I would like to
be able to do something similar to this:
http://www.wineanswers.com/perfectPairings.aspx where you can choose a
wine to get foods and sauces that go with that wine or choose a sauce
and find out which wines and foods go with it, etc. To accomplish this,
I thought I would be able to set up a simple has_many though
relationship like this:

Migrations:

class CreateFoods < ActiveRecord::Migration
  def self.up
    create_table :foods do |t|
      t.string :name
    end
  end
end

class CreateSauces < ActiveRecord::Migration
  def self.up
    create_table :sauces do |t|
      t.string :name
    end
  end
end

class CreateWines < ActiveRecord::Migration
  def self.up
    create_table :wines do |t|
      t.string :name
      t.integer :sauce_id
      t.integer :food_id
    end
  end
end


Models:

class Wine < ActiveRecord::Base
  belongs_to :sauce
  belongs_to :food
end

class Food < ActiveRecord::Base
  has_many :wines
  has_many :sauces, :through => :wines
end

class Sauce < ActiveRecord::Base
  has_many :wines
  has_many :foods, :through => :wines
end


While I can use the console to find food.sauces and sauce.foods and loop
through the results no problem, I cannot get an array from wine.foods or
wine.sauces. I can get wine.food which returns only the first item of
what I thought would be an array foods attached to that wine. I am not
sure how I can get the result I am looking for with the wine model. How
can I get the array of foods or sauces attached to a particular wine?
Should I be using a habtm join for the wine-food and wine-sauce
relationships? But then how would I get the food and sauces models to
talk to each other?

Any help in understanding a bit more about how to properly join these
three models would be greatly appreciated.


Thanks,

Nate

_______________________________________________
Radiant mailing list
Post:   Radiant@radiantcms.org
Search: http://radiantcms.org/mailing-list/search/
Site:   http://lists.radiantcms.org/mailman/listinfo/radiant


_______________________________________________
Radiant mailing list
Post:   Radiant@radiantcms.org
Search: http://radiantcms.org/mailing-list/search/
Site:   http://lists.radiantcms.org/mailman/listinfo/radiant

Reply via email to