使用collection.build时未创建belongs_to关联

发布于 2024-11-15 11:29:20 字数 640 浏览 1 评论 0原文

我有这样的形式:

<%= form_for(@debate.debates.build) do |support_form| %>  
  <div>
    <%= support_form.label :content %><br />
    <%= support_form.text_area :content %>
  </div>
  <%= support_form.hidden_field :is_supporting, :value => is_supporting %>
  <div class="actions">
    <%= support_form.submit %>
  </div>
<% end %>

每个辩论都有_许多辩论,并且属于一个辩论(树结构),并且@debate.debates.build应该创建一个新的辩论,它是@debate的子项,但是由@debate.debates创建的辩论.build 总是 nil

当我在 irb 中运行相同的代码时,关联已正确设置,并且新辩论的辩论 id 是其父级,这正是我希望的方式。

这是怎么回事?我如何确保新辩论的父辩论 ID 设置正确?

I have this form:

<%= form_for(@debate.debates.build) do |support_form| %>  
  <div>
    <%= support_form.label :content %><br />
    <%= support_form.text_area :content %>
  </div>
  <%= support_form.hidden_field :is_supporting, :value => is_supporting %>
  <div class="actions">
    <%= support_form.submit %>
  </div>
<% end %>

Each debate has_many debate and belongs to a debate (a tree structure) and @debate.debates.build is supposed to create a new debate that is the child of @debate, but the debate created by @debate.debates.build is always nil

When I run the same code in irb, though, the association is correctly set up, and the debate id of the new debate is its parent, the way I want it to be.

Whats going on? And how can I make sure that the new debate has its parent debates id set up correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

痴情换悲伤 2024-11-22 11:29:20

它在内存中构建它,但我认为它实际上不会保存它,直到运行保存,大概是在表单的接收操作中,可能是控制器的创建操作。

为了使其发挥作用,辩论 ID(debate_id?您没有显示足够的模型)可能也需要采用表单。

最后,该问题可能与 attr_accessibleattr_protected 问题有关。如果不允许将辩论 ID 设置为批量属性更新的一部分,则它可能会在提交中丢失。

It builds it in memory, but I don't think it actually saves it until a save is run, presumably in the recipient action of the form, probably the create action of the controller.

In order for that to work, the debate id (debate_id? you don't show enough of your model) may need to be in the form as well.

Finally, the issue could be related to attr_accessible or attr_protected issues. If the debate id is not allowed to be set as part of the bulk attribute update, it may get lost in the submission.

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