Ruby-on-rails 如何将记录添加到 has_many :通过 rails 中的关联

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7297338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 01:55:13  来源:igfitidea点击:

how to add records to has_many :through association in rails

ruby-on-railsactiverecordhas-many-through

提问by Mike

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 Agentsmodel for Customer?

如何添加到Agents模型中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_idas input. Then do I do the following in my controller?

想象一下,为客户填写了一个表单,该表单也house_id作为输入。然后我在我的控制器中执行以下操作吗?

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 :throughtable?

总的来说,我对如何在has_many :through表中添加记录感到困惑?

回答by Mischa

I think you can simply do this:

我认为你可以简单地这样做:

 @cust = Customer.new(params[:customer])
 @cust.houses << House.find(params[:house_id])

Or when creating a new house for a customer:

或者在为客户建造新房子时:

 @cust = Customer.new(params[:customer])
 @cust.houses.create(params[:house])

You can also add via ids:

您还可以通过 id 添加:

@cust.house_ids << House.find(params[:house_id])

回答by IAmNaN

'The best way' depends on your needs and what feels most comfortable. Confusion comes from differences ActiveRecord's behavior of the newand createmethods and the <<operator.

“最佳方式”取决于您的需求和感觉最舒适的方式。混乱来自不同的ActiveRecord的行为newcreate方法和<<操作。

The newMethod

new方法

newwill not add an association record for you. You have to build the Houseand Agentrecords yourself:

new不会为您添加关联记录。您必须自己构建HouseAgent记录:

house = @cust.houses.new(params[:house])
house.save
agent = Agent(customer_id: @cust.id, house_id: house.id)
agent.save

Note that @cust.houses.newand House.neware effectively the same because you need to create the Agentrecord in both cases.

请注意,@cust.houses.newHouse.new实际上是相同的,因为您需要Agent在两种情况下都创建记录。

The <<Operator

<<运营商

As Mischa mentions, you can also use the <<operator on the collection. This will only build the Agentmodel for you, you must build the Housemodel:

正如 Mischa 提到的,您还可以<<在集合上使用运算符。这只会Agent为您构建模型,您必须构建House模型:

house = House.create(params[:house])
@cust.houses << house
agent = @cust.houses.find(house.id)

The createMethod

create方法

createwill build both Houseand Agentrecords for you, but you will need to find the Agentmodel if you intend to return that to your view or api:

create将为您构建HouseAgent记录,但Agent如果您打算将其返回到您的视图或 api ,您将需要找到模型:

house = @cust.houses.create(params[:house])
agent = @cust.agents.where(house: house.id).first

As a final note, if you want exceptions to be raised when creating houseuse the bang operators instead (e.g. new!and create!).

最后要注意的是,如果您希望在创建时引发异常,请house改用 bang 运算符(例如new!create!)。

回答by Dennis

Another way to add associations is by using the foreign key columns:

添加关联的另一种方法是使用外键列:

agent = Agent.new(...)
agent.house = House.find(...)
agent.customer = Customer.find(...)
agent.save

Or use the exact column names, passing the ID of the associated record instead of the record.

或者使用确切的列名,传递关联记录的 ID 而不是记录。

agent.house_id = house.id
agent.customer_id = customer.id