简体   繁体   English

在Rails中正确设置has_many

[英]Setting up has_many through correctly in Rails

Yesterday I asked a question about setting up has_and_belongs_to_many relationships between models in Rails. 昨天我问了一个有关在Rails中的模型之间设置has_and_belongs_to_many关系的问题。 I eventually got those relationships working, however, I realized that I needed to store additional information in my join table, which means I should be using a has_many through relationship with a join model (I presume?). 我最终使这些关系正常工作,但是,我意识到我需要在联接表中存储其他信息,这意味着我应该通过与联接模型的关系来使用has_many(我想?)。

I now have a Recipe model, Ingredient model, and an Amounts join model. 我现在有一个配方模型,成分模型和一个数量联接模型。 Each Recipe has many Ingredients and vice versa. 每个食谱都有很多成分,反之亦然。 And each Ingredient in a recipe has a certain Amount in a certain unit type. 配方中的每种成分在某种单位类型中都有一定数量。

Here is my setup so far: 到目前为止,这是我的设置:

models/recipe.rb 模型/recipe.rb

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

models/ingredient.rb 型号/ingredient.rb

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

models/amounts.rb 型号/数量.rb

class Amounts < ActiveRecord::Base
  belongs_to :recipes
  belongs_to :ingredients
end

MySQL tables: MySQL表:

$ mysql > show columns from recipes;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| name       | varchar(255) | NO   |     | NULL    |                |
| desc       | text         | NO   |     | NULL    |                |
| image_url  | varchar(255) | YES  |     | NULL    |                |
| created_at | datetime     | NO   |     | NULL    |                |
| updated_at | datetime     | NO   |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

mysql> show columns from ingredients;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| name       | varchar(255) | NO   |     | NULL    |                |
| created_at | datetime     | NO   |     | NULL    |                |
| updated_at | datetime     | NO   |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

mysql> show columns from amounts;
+---------------+--------------+------+-----+---------+----------------+
| Field         | Type         | Null | Key | Default | Extra          |
+---------------+--------------+------+-----+---------+----------------+
| id            | int(11)      | NO   | PRI | NULL    | auto_increment |
| recipe_id     | int(11)      | NO   |     | NULL    |                |
| ingredient_id | int(11)      | NO   |     | NULL    |                |
| amount        | int(11)      | NO   |     | NULL    |                |
| units         | varchar(255) | NO   |     | NULL    |                |
| created_at    | datetime     | NO   |     | NULL    |                |
| updated_at    | datetime     | NO   |     | NULL    |                |
+---------------+--------------+------+-----+---------+----------------+

I think I have the relationships set up correctly...I'm just unsure about how to add to the join model/table correctly and then access those values. 我想我已经正确设置了关系...我只是不确定如何正确添加到联接模型/表中,然后访问这些值。 For instance, will I be able to call @recipe.ingredients and return all the ingredients associated? 例如,我可以调用@ recipe.ingredients并返回所有关联的成分吗?

EDIT: I apologize for the confusion over what I am trying to do. 编辑:对于我要做什么的困惑,我深表歉意。

When I was using the has_and_belongs_to_many relationship, I could push to @recipe.ingredients with '<<' and have the join table automatically inserted with the recipe_id and ingredient_id. 当我使用has_and_belongs_to_many关系时,我可以使用'<<'推送到@ recipe.ingredients,并自动将joint表与recipe_id和Ingredient_id插入。 However, this method doesn't seem to work when using has_many through because of the additional attributes in the join table. 但是,由于使用了连接表中的其他属性,因此在使用has_many through时,此方法似乎不起作用。

How would I go about creating these associations? 我将如何创建这些关联?

you can read and assign to recipe.ingredients directly and it will automatically write to the join table. 您可以直接读取并分配给recipe.ingredients ,它将自动写入连接表。

But unlikely you will need it since you need additional info for the amount. 但是您不太可能需要它,因为您需要该数量的其他信息。 You probably will need to update/create recipes with nested attributes for amounts coming from a form. 您可能需要使用嵌套属性为表单中的数量更新/创建配方。

consider a uniqueness contraint on ingredients per recipe in amount model. 在数量模型中,考虑每个配方成分的唯一性约束。

hope this helped 希望这有所帮助

Setting up a has_many :through association is some of that Rails magic. 建立has_many :through关联是Rails的一些魔术。 You'll have access to all the methods that the has_many association sets up for both the join table, and the related table. 您将有权访问has_many关联为联接表和相关表设置的所有方法。 Rails will take care of any associations automatically. Rails会自动处理所有关联。

Have a look at the Rails guide here . 这里看看Rails指南。

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

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