Rails的:用的has_many额外的细节? [英] Rails: has_many with extra details?
问题描述
虽然我不是一个完整的Ruby / Rails的福利局,我还是pretty的绿色,我试图找出如何构建一些模型关系。我能想到的最简单的例子是食谱做饭的想法。
一个配方由一种或多种成分和每种成分的相关数量。假设我们有一个主列表中的所有成分的数据库。这表明两个简单的模型:
类成分<的ActiveRecord :: Base的
#成份名称,
结束
一流的配方<的ActiveRecord :: Base的
#配方名称等。
结束
如果我们只是想配方与成份相关联,这是因为simpling如添加适当的 belongs_to的
和的has_many
。
但是,如果我们要与这种关系的其他信息联系起来呢?每个配方
有一个或多个成分
,但我们要指出的成分的量
。
什么是Rails的方式来模拟?它是沿着的has_many的东西线,通过
?
类成分<的ActiveRecord :: Base的
#成份名称
belongs_to的:recipe_ingredient
结束
类RecipeIngredient<的ActiveRecord :: Base的
HAS_ONE:成分
HAS_ONE:食谱
# 数量
结束
一流的配方<的ActiveRecord :: Base的
的has_many:recipe_ingredients
的has_many:配料,:通过=> :recipe_ingredients
结束
配方和配料都有了属于一对多的关系,但是你想存储更多信息的链接。
您正在寻找本质上讲是一个丰富的连接模型。但是,一个has_and_belongs_to_many关系是不够灵活,无法存储您所需要的其他信息。相反,你将需要使用的has_many:通过relatinship
这是我怎么会设置它。
菜谱列:说明
类食谱<的ActiveRecord :: Base的
的has_many:recipe_ingredients
的has_many:配料,:通过=> :recipe_ingredients
结束
recipe_ingredients列:recipe_id,ingredient_id,量
类RecipeIngredients<的ActiveRecord :: Base的
belongs_to的:食谱
belongs_to的:成分
结束
成分列:名称
类成分<的ActiveRecord :: Base的
的has_many:recipe_ingredients
的has_many:食谱,:通过=> :recipe_ingredients
结束
这将为你在找什么做的基本的重新presentation。您可能需要验证添加到RecipeIngredients,以确保每个成分列出一次每个配方,并回调折叠重复到一个条目中。
While I'm not a complete Ruby/Rails newb, I'm still pretty green and I'm trying to figure out how to structure some model relationships. The simplest example I can think of is the idea of "recipes" for cooking.
A recipe consists of one or more ingredients and the associated quantity of each ingredient. Assume we have a master list in the database of all ingredients. That suggests two simple models:
class Ingredient < ActiveRecord::Base
# ingredient name,
end
class Recipe < ActiveRecord::Base
# recipe name, etc.
end
If we just wanted to associate Recipes with Ingredients, that's as simpling as adding the appropriate belongs_to
and has_many
.
But what if we want to associate additional information with that relationship? Each Recipe
has one or more Ingredients
, but we want to indicate the quantity of the Ingredient
.
What is the Rails way to model that? Is it something along the lines of a has_many through
?
class Ingredient < ActiveRecord::Base
# ingredient name
belongs_to :recipe_ingredient
end
class RecipeIngredient < ActiveRecord::Base
has_one :ingredient
has_one :recipe
# quantity
end
class Recipe < ActiveRecord::Base
has_many :recipe_ingredients
has_many :ingredients, :through => :recipe_ingredients
end
Recipes and Ingredients have a has and belongs to many relationship, but you want to store additional information for link.
Essentially what you are looking for is a rich join model. But, a has_and_belongs_to_many relationship is not flexible enough to store the additional information you require. Instead you will need to use a has_many :through relatinship.
This is how I would set it up.
recipes columns: instructions
class Recipe < ActiveRecord::Base
has_many :recipe_ingredients
has_many :ingredients, :through => :recipe_ingredients
end
recipe_ingredients columns: recipe_id, ingredient_id, quantity
class RecipeIngredients < ActiveRecord::Base
belongs_to :recipe
belongs_to :ingredient
end
ingredient columns: name
class Ingredient < ActiveRecord::Base
has_many :recipe_ingredients
has_many :recipes, :through => :recipe_ingredients
end
This will provide a basic representation of what you're looking to do. You may want to add a validation to RecipeIngredients to ensure that each ingredient is listed once per recipe, and a callback to fold duplicates into one entry.
这篇关于Rails的:用的has_many额外的细节?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!