简体   繁体   English

在Rails中,如何将多个外键添加到同一模型中? 我有等效的Django

[英]In Rails, how do you add multiple foreign keys to the same model? I have the Django equivalent

I'm using Rails 3 Beta and I assume the syntax is similar to the 2.x. 我正在使用Rails 3 Beta,并且我认为语法类似于2.x。 I'm also not very familiar with Ruby and Rails. 我对Ruby和Rails也不太熟悉。

In Django, multiple foreign keys to the same model looks like the following: 在Django中,指向同一模型的多个外键如下所示:

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: 在Rails中,我正在考虑执行以下操作:

# 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. 每道菜最多可以包含一个午餐成分对象和最多一个晚餐成分对象。

Its not clear from your model if there is a 1-to-1 or 1-to-n relationship amongst Dish model and Ingredients model. 从您的模型尚不清楚, Dish模型和Ingredients模型之间是否存在1-to-11-to-n关系。 If the relationship is 1-to-1 , following code should work: 如果关系是1-to-1 ,则以下代码应该起作用:

class Dish < ActiveRecord::Base
 has_one :lunch, :class_name => 'Ingredient', :conditions => {:meal => 'lunch'}
 has_one :dinnner, :class_name => 'Ingredient', :conditions => {:meal => 'dinner'}

end
# now you can get lunch and dinner using the calls below on a Dish object.
dish.lunch
dish.lunch

If the relationship is 1-to-n , following code should work: 如果该关系是1-to-n ,则以下代码应工作:

class Dish < ActiveRecord::Base
 has_many :lunches, :class_name => 'Ingredient', :conditions => {:meal => 'lunch'}
 has_many :dinnners, :class_name => 'Ingredient', :conditions => {:meal => 'dinner'}
end

Firstly for me it doesn't look good if you use string column to store only two types of something. 首先,对我而言,如果您使用字符串列仅存储两种类型的内容,则效果并不理想。 You can store it as boolean or as integer when there are more types of meals. 当膳食种类更多时,可以将其存储为布尔值或整数。 You can add an array that maps meal type id to lunch or dinner or anything else. 您可以添加一个将膳食类型ID映射到lunchdinner或其他任何东西的数组。

# Ingredient model
belongs_to :dish

def meal
  MEAL_TYPES[meal_id]
end

private
MEAL_TYPES = ['lunch', 'dinner']


# Dish model
has_one :lunch, :class_name => 'Ingredient', :conditions => {:meal_id => 0}
has_one :dinner, :class_name => 'Ingredient', :conditions => {:meal_id => 1}

Then in your code you can use it as fallows: 然后可以在代码中将其用作休假:

@dish = Dish.find(params[:id])
@dish.lunch # returns lunch ingredients
@dish.dinner # returns dinner ingredients

@dish.lunch.meal # => "lunch"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM