如何在rails 3.1中的多对多关系的联合表中保存2个id [英] How to save 2 id in joint table for many-to-many relationship in rails 3.1

查看:44
本文介绍了如何在rails 3.1中的多对多关系的联合表中保存2个id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有两种型号.一种是 rfq,另一种是标准的.就像:

There are two models. One is rfq and another one is standard. It is like:

class Rfq << ActiveRecord::Base
  has_and_belongs_to_many :standards
end

class Standard << ActiveRecord::Base
  has_and_belongs_to_many :rfqs
end

表 rfqs_standards 已创建.我的问题是在创建rfq时,如何自动保存rfqs_standards表中rfq_id和standard_id的payment.

Table rfqs_standards has been created. My question is when creating rfq, how to save the paid of rfq_id and standard_id in table rfqs_standards automatically.

在 rfq 模型中添加 accepts_nested_attributes_for :standard 的想法.然而,由于没有为这种多对多关系保存真正的属性(而只是一对 id),这似乎不是正确的方法.

Thought adding accepts_nested_attributes_for :standard in rfq model. However since there is no real attributes (but just pair of id) saved for this many-to-many relationship, this seems not the right way.

rfq 和标准都在 routes.rb 中声明为 resources :rfqsresources :standards.

Both rfq and standard was declared in routes.rb as resources :rfqs and resources :standards.

程序是在创建 rfq 时,将通过下拉列表选择标准.然后保存rfq,同时在联合表中创建一个新条目.创建新标准时,无需在联合表中创建条目.

The procedure is when creating rfq, standard will be picked up via a drop down list. Then rfq is saved and at the same time, a new entry in joint table is created. When creating new standard, there is no need to create entry in joint table.

有什么建议可以将 id 保存在联合表中吗?谢谢.

Any suggestion to save the id in joint table? Thanks.

推荐答案

这比您想象的要容易,因为它是由 ActiveRecord 自动处理的.

this is easier than you might think because it's handled automatically by ActiveRecord.

当您说has_and_belongs_to_many"时,您是在告诉 AR 使用该表将这两个模型与多对多关系相关联,并且在大多数情况下您不再需要担心连接表.当您将 Standard 的实例添加到 Rfq 的标准列表时,这将为您完成.

When you say "has_and_belongs_to_many", you're telling AR to associate those two models with a many-to-many relationship using the table, and for the most part you no longer need to worry about the join table. When you add an instance of Standard to an Rfq's list of standards, this will be done for you.

这是一个例子:

rfq = Rfq.create
standard = Standard.create
rfq.standards << standard

我们已经创建了每个对象,第三行创建了连接,在 rfqs_standards 表中保存了一个具有正确 id 的新记录.rqf.standards 看起来和行为就像一个普通的数组,但是当你给它分配对象时,ActiveRecord 会为你做数据库工作.

We've created each of the objects, and the third line creates the connection, saving a new record in the rfqs_standards table with the proper ids. rqf.standards looks and acts like a normal array, but when you assign objects to it, ActiveRecord does the database work for you.

创建记录后,您还可以:

After creating the records, you could have also done:

standard.rfqs << rfq

您也可以同时进行:

rfq = Rfq.create
standard rfq.standards.create

这创建了一个 rfq,然后创建了一个自动连接到 rfq 的标准.你可以反过来做同样的事情:

This created an rfq, then created a standard that is automatically connected to the rfq. You can do the same thing in reverse:

standard = Standard.create
rfq = standard.rfqs.create

我希望这会有所帮助!

更新:既然你提到了表单和自动保存,请阅读我关于 嵌套属性,展示了如何实现,包括完整的代码示例.

UPDATE: Since you mentioned forms and automatic saving, read my article on nested attributes that shows how to implement that, including full code samples.

这篇关于如何在rails 3.1中的多对多关系的联合表中保存2个id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆