I'm using Rails 3 Beta and I assume the syntax is similar to the 2.x. I'm also not very familiar with Ruby and Rails.
In Django, multiple foreign keys to the same model looks like the following:
class Dish(models.Model):
name = models.CharField(max_length=100)
lunch = models.ForeignKey(Ingredient, related_name='lunch')
dinner = models.ForeignKey(Ingredient, related_name='dinner')
class Ingredient(models.Model):
spices = models.IntegerField()
veggies = models.IntegerField()
In Rails, I'm thinking of doing something like the following:
# Migration file
create_table :dishes do |t|
t.column :name, :string
end
create_table :ingredients do |t|
t.column :spice, :integer
t.column :veggies, :integer
t.column: :dish_id, :integer
t.column: :meal, :string # lunch or dinner
end
# Models
class Dish < ActiveRecord::Base
def lunch
return # ingredient for the current dish where type == lunch
end
def dinner
return # ingredient for the current dish where type == dinner
end
end
Is the above the right idea or is there a better way to do it?
More Clarifications: The restaurant serves the same dish during both lunch and dinner but uses different amount of ingredient between those two meal times. Each dish can contain at most one lunch ingredient object and at most one dinner ingredient object.