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

发布于 2024-12-03 00:07:42 字数 850 浏览 3 评论 0原文

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

如何添加到 CustomerAgents 模型?

这是最好的方法吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜柠檬 2024-12-10 00:07:42

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

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

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

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

你也可以通过 ids 添加:

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

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:

@cust.house_ids << House.find(params[:house_id])
望笑 2024-12-10 00:07:42

前言

这是一个奇怪的场景,我犹豫着要不要回答。看起来 Agent 应该有许多 House 而不是一对一的关系,而一个 House 应该只属于一个<代码>代理。但考虑到这一点......

“最好的方法”取决于您的需求以及您感觉最舒服/最可读的方式。混乱来自于 ActiveRecord 的 new 和 create 方法以及 << 运算符的行为差异,但它们都可以用来完成你的目标。

new 方法

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

# ...
house = @cust.houses.new(params[:house])
house.save
agent = Agent.new(customer: @cust house: house)
agent.save

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

(这段代码看起来很奇怪,你无法轻易说出它应该做什么,而且这可能是关系设置错误的味道。)

<< 运算符

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

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

create 方法

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

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

最后一点,如果您希望在创建时引发异常house 使用 bang 运算符(例如 new!create!)。

Preface

This is a strange scenario and I hesitated to answer. It seems like Agents should have many Houses rather than a one-to-one relationship, and a House should belong to just one Agent. But with that in mind....

"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 and create methods and the << operator, but they can all be used to accomplish your goal.

The new Method

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

# ...
house = @cust.houses.new(params[:house])
house.save
agent = Agent.new(customer: @cust house: house)
agent.save

Note that @cust.houses.new and House.new are effectively the same because you still need to create the Agent 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 << 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:

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:

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!).

蓝天 2024-12-10 00:07:42

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

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

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

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

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.

agent.house_id = house.id
agent.customer_id = customer.id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文