Rails:has_many 有额外的细节吗?

发布于 2024-08-18 05:47:14 字数 933 浏览 5 评论 0原文

虽然我不是一个完整的 Ruby/Rails 新手,但我仍然很新手,我正在尝试弄清楚如何构建一些模型关系。我能想到的最简单的例子是烹饪“食谱”的想法。

配方由一种或多种成分以及每种成分的相关数量组成。假设我们的数据库中有所有成分的主列表。这表明了两个简单的模型:

class Ingredient < ActiveRecord::Base
  # ingredient name, 
end

class Recipe < ActiveRecord::Base
  # recipe name, etc.
end

如果我们只想将食谱与成分关联起来,那么就像添加适当的 belongs_tohas_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扎心 2024-08-25 05:47:14

食谱和成分具有“拥有”和“属于”多种关系,但您希望存储链接的附加信息。

本质上,您正在寻找的是丰富的连接模型。但是,has_and_belongs_to_many 关系不够灵活,无法存储您需要的附加信息。相反,您需要使用 has_many :through 关系。

这就是我要设置的方式。

食谱列:说明

class Recipe < ActiveRecord::Base
  has_many :recipe_ingredients
  has_many :ingredients, :through => :recipe_ingredients
end

recipe_ingredients 列:recipe_id、recipe_id、数量

class RecipeIngredients < ActiveRecord::Base
  belongs_to :recipe
  belongs_to :ingredient
end

成分列:名称

class Ingredient < ActiveRecord::Base
  has_many :recipe_ingredients
  has_many :recipes, :through => :recipe_ingredients
end

这将提供您要执行的操作的基本表示。您可能需要向 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

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.

探春 2024-08-25 05:47:14

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

怎么样:

  1. class Ingredient (属于recipe,有很多成分recipecounts)
  2. class Recipe (有很多成分,有很多成分recipecounts)
  3. class IngredientRecipeCount (属于recipe,属于recipecount)

这与其说是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:

  1. class Ingredient (belongs to recipe, has many ingredientrecipecounts)
  2. class Recipe (has many ingredients, has many ingredientrecipecounts)
  3. class IngredientRecipeCount (belongs to ingredient, belongs to recipe)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文