如何将记录添加到 has_many :通过rails中的关联
class Agents << ActiveRecord::Base
belongs_to :customer
belongs_to :house
end
class Customer << ActiveRecord::Base
has_many :agents
has_many :houses, through: :agents
end
class House << ActiveRecord::Base
has_many :agents
has_many :customers, through: :agents
end
如何添加到 Customer
的 Agents
模型?
这是最好的方法吗?
Customer.find(1).agents.create(customer_id: 1, house_id: 1)
上面的内容在控制台上运行良好,但是我不知道如何在实际应用程序中实现这一点。
想象一下,为客户填写的表单也将 house_id
作为输入。那么我要在控制器中执行以下操作吗?
def create
@customer = Customer.new(params[:customer])
@customer.agents.create(customer_id: @customer.id, house_id: params[:house_id])
@customer.save
end
总的来说,我对如何在 has_many :through
表中添加记录感到困惑?
class Agents << ActiveRecord::Base
belongs_to :customer
belongs_to :house
end
class Customer << ActiveRecord::Base
has_many :agents
has_many :houses, through: :agents
end
class House << ActiveRecord::Base
has_many :agents
has_many :customers, through: :agents
end
How do I add to the Agents
model for Customer
?
Is this the best way?
Customer.find(1).agents.create(customer_id: 1, house_id: 1)
The above works fine from the console however, I don't know how to achieve this in the actual application.
Imagine a form is filled for the customer that also takes house_id
as input. Then do I do the following in my controller?
def create
@customer = Customer.new(params[:customer])
@customer.agents.create(customer_id: @customer.id, house_id: params[:house_id])
@customer.save
end
Overall I'm confused as to how to add records in the has_many :through
table?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为你可以简单地这样做:
或者在为客户创建新房子时:
你也可以通过 ids 添加:
I think you can simply do this:
Or when creating a new house for a customer:
You can also add via ids:
“最好的方法”取决于您的需求以及您感觉最舒服/最可读的方式。混乱来自于 ActiveRecord 的 new 和 create 方法以及
<<
运算符的行为差异,但它们都可以用来完成你的目标。new
方法new
不会为您添加关联记录。您必须自己构建House
和Agent
记录:请注意
@cust.houses.new
和House.new
实际上是相同的,因为在这两种情况下您仍然需要创建Agent
记录。(这段代码看起来很奇怪,你无法轻易说出它应该做什么,而且这可能是关系设置错误的味道。)
<<
运算符正如 Mischa 提到的,您还可以在集合上使用
<<
运算符。这只会为您构建Agent
模型,您必须构建House
模型:create
方法create
将为您构建House
和Agent
记录,但如果您打算将其返回到视图或 api,则需要找到Agent
模型:最后一点,如果您希望在创建时引发异常
house
使用 bang 运算符(例如new!
和create!
)。"The best way" depends on your needs and what feels most comfortable/readable to you. Confusion comes from differences in ActiveRecord's behavior of the
new
andcreate
methods and the<<
operator, but they can all be used to accomplish your goal.The
new
Methodnew
will not add an association record for you. You have to build theHouse
andAgent
records yourself:Note that
@cust.houses.new
andHouse.new
are effectively the same because you still need to create theAgent
record in both cases.(This code looks weird, you can't easily tell what it's supposed to be doing, and that's a smell that maybe the relationships are set up wrong.)
The
<<
OperatorAs Mischa mentions, you can also use the
<<
operator on the collection. This will only build theAgent
model for you, you must build theHouse
model:The
create
Methodcreate
will build bothHouse
andAgent
records for you, but you will need to find theAgent
model if you intend to return that to your view or api:As a final note, if you want exceptions to be raised when creating
house
use the bang operators instead (e.g.new!
andcreate!
).添加关联的另一种方法是使用外键列:
或者使用确切的列名称,传递关联记录的 ID 而不是记录本身。
Another way to add associations is by using the foreign key columns:
Or use the exact column names, passing the ID of the associated record instead of the record.