如何向has_many添加记录:通过rails中的关联

时间:2021-04-30 20:09:59
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?

假设一个表单是为同样接受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 :through table?

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

3 个解决方案

#1


126  

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])

#2


69  

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

“最好的方法”取决于你的需要和什么感觉最舒服。混淆来自于ActiveRecord的新方法和创建方法和< <操作符。< p>

The new Method

new will not add an association record for you. You have to build the House and Agent records yourself:

new不会为您添加关联记录。你得建房子,经纪人自己记录:

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

Note that @cust.houses.new and House.new are effectively the same because you need to create the Agent record in both cases.

注意:@cust.houses。新房子。new实际上是相同的,因为您需要在这两种情况下创建代理记录。

The << Operator

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

正如Mischa提到的,您还可以在集合上使用 <运算符。这只会为你建立代理模型,你必须建立房子模型:< p>

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

The create Method

create will build both House and Agent records for you, but you will need to find the Agent model if you intend to return that to your view or api:

create将为您构建House和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 house use the bang operators instead (e.g. new! and create!).

最后,如果您希望在创建house时引起异常,请使用bang操作符(例如new!)并创建)。

#3


5  

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

#1


126  

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])

#2


69  

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

“最好的方法”取决于你的需要和什么感觉最舒服。混淆来自于ActiveRecord的新方法和创建方法和< <操作符。< p>

The new Method

new will not add an association record for you. You have to build the House and Agent records yourself:

new不会为您添加关联记录。你得建房子,经纪人自己记录:

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

Note that @cust.houses.new and House.new are effectively the same because you need to create the Agent record in both cases.

注意:@cust.houses。新房子。new实际上是相同的,因为您需要在这两种情况下创建代理记录。

The << Operator

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

正如Mischa提到的,您还可以在集合上使用 <运算符。这只会为你建立代理模型,你必须建立房子模型:< p>

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

The create Method

create will build both House and Agent records for you, but you will need to find the Agent model if you intend to return that to your view or api:

create将为您构建House和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 house use the bang operators instead (e.g. new! and create!).

最后,如果您希望在创建house时引起异常,请使用bang操作符(例如new!)并创建)。

#3


5  

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