Rails:has_many 有额外的细节吗?
虽然我不是一个完整的 Ruby/Rails 新手,但我仍然很新手,我正在尝试弄清楚如何构建一些模型关系。我能想到的最简单的例子是烹饪“食谱”的想法。
配方由一种或多种成分以及每种成分的相关数量组成。假设我们的数据库中有所有成分的主列表。这表明了两个简单的模型:
class Ingredient < ActiveRecord::Base
# ingredient name,
end
class Recipe < ActiveRecord::Base
# recipe name, etc.
end
如果我们只想将食谱与成分关联起来,那么就像添加适当的 belongs_to
和 has_many
一样简单。
但是如果我们想要将附加信息与该关系关联起来怎么办?每个Recipe
都有一种或多种Ingredients
,但我们想指明Ingredient
的数量。
Rails 的建模方法是什么?它是类似于 has_many 到
的东西吗?
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
食谱和成分具有“拥有”和“属于”多种关系,但您希望存储链接的附加信息。
本质上,您正在寻找的是丰富的连接模型。但是,has_and_belongs_to_many 关系不够灵活,无法存储您需要的附加信息。相反,您需要使用 has_many :through 关系。
这就是我要设置的方式。
食谱列:说明
recipe_ingredients 列:recipe_id、recipe_id、数量
成分列:名称
这将提供您要执行的操作的基本表示。您可能需要向 RecipeIngredients 添加验证,以确保每个配方中的每种成分都列出一次,并添加一个回调以将重复项折叠到一个条目中。
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
recipe_ingredients columns: recipe_id, ingredient_id, quantity
ingredient columns: name
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.
http://railsbrain.com/ api/rails-2.3.2/doc/index.html?a=M001888&name=has_and_belongs_to_many
http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M001885&name=has_many
怎么样:
这与其说是Rails方式,不如说只是建立了一种关系数据库中的数据之间。这并不是真正的“拥有并属于许多”,因为每种成分每个食谱只有一个计数,而每个食谱每种成分一个计数..这是相同的计数。
http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M001888&name=has_and_belongs_to_many
http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M001885&name=has_many
How about:
This is not so much the Rails way as just establishing one more relation between the data in the database. It's not really a "has and belongs to many" because each ingredient only has one count per recipe, and each recipe one count per ingredient.. Which is the same count.