Advie for model relationships

I have 3 models, let’s call them Recipe, Fruit and Nut, and I need a
DRY way to set up their relationships.

Fruits and Nuts have different sets of properties
Fruits and Nuts go into a Recipe
A join model would be useful to track how much Fruit or Nut goes into a
Recipe
Different Recipes may use a Nut with the name, but different property
values
I might want to add a Twig or Llama model to my Recipes

Right now I have a separate join models that do pretty much the same
for each of Fruit and Nut, but it’s getting really wet (opposite of
DRY). It works just fine, but the code is getting really redundant.

Untill I can hack rails so has_many :through works with polymorphic
associations, what is a good way to do this?

class Recipe < AR::B
has_many :fruitadditions
has_many :fruits, :through => :fruitaddition

has_many :nutadditions
has_many :nuts, :through => :nutaddition
end

class Fruit < AR::B
has_many :fruitadditions
has_many :recipes, :through => :fruitadditions
end

class Nut < AR::B
has_many :nutadditions
has_many :recipes, :through => :nutadditions
end

TIA

–Dean

So I thought I would share the solution I came up with.

class Recipe < AR::B
has_many :fruitadditions, :conditions => [ ‘fruit_id IS NOT NULL’ ]
has_many :fruits, :through => :fruitaddition

has_many :nutadditions, :conditions => [ ‘nut_id IS NOT NULL’ ]
has_many :nuts, :through => :nutaddition
end

class Ingredient < ActiveRecord::Base
belongs_to :recipe

def some_methods
# …
end
end

class Fruitingredient < Ingredient
belongs_to :fruit
end

class Fruit < ActiveRecord::Base
has_many :fruitingredients
has_many :recipes, :through => :fruitingredients
end

class Twigingredient < Ingredient
belongs_to :twig
end

class Twig < ActiveRecord::Base
has_many :twigingredients
has_many :recipes, :through => :twigingredients
end

So I have a single ingredients table with the FKs for each separate
ingredient table. It seems just a little hack-ey, but works well.

–Dean