Rails,使用 has_many through 和 fields_for

发布于 2024-10-11 09:14:14 字数 1193 浏览 3 评论 0原文

我在让它发挥作用时遇到了一些麻烦。

class User < ActiveRecord::Base
  has_many :events, :through => :event_users
  has_many :event_users
  accepts_nested_attributes_for :event_users, :allow_destroy => true, :reject_if => proc { |obj| obj.blank? }
end

class Event < ActiveRecord::Base
  has_many :event_users
  has_many :users, :through => :event_users
  accepts_nested_attributes_for :users, :reject_if => lambda { |a| a[:nick].blank? }, :allow_destroy => true
end

class EventUser < ActiveRecord::Base
  set_table_name :events_users
  belongs_to :event
  belongs_to :user
end

表布局:

events_users
  user_id
  event_id
  is_participating

events
  id
  name

users
  id
  name

这是表单的代码

<%= form_for @event do |f| %>
  <%= f.fields_for :users, f.object.users do |builder| %>
    <%= builder.text_field :name, "Name" %>
    <%= f.fields_for :event_users do |builder2| %>
      <%= builder2.hidden_field :is_participating, :value => true %>
    <% end %>
  <% end %>
<% end %>

我想要完成的是在 events_users 表中设置字段 is_participating,但它不起作用!

I have a little trouble getting this to work.

class User < ActiveRecord::Base
  has_many :events, :through => :event_users
  has_many :event_users
  accepts_nested_attributes_for :event_users, :allow_destroy => true, :reject_if => proc { |obj| obj.blank? }
end

class Event < ActiveRecord::Base
  has_many :event_users
  has_many :users, :through => :event_users
  accepts_nested_attributes_for :users, :reject_if => lambda { |a| a[:nick].blank? }, :allow_destroy => true
end

class EventUser < ActiveRecord::Base
  set_table_name :events_users
  belongs_to :event
  belongs_to :user
end

Table-layout:

events_users
  user_id
  event_id
  is_participating

events
  id
  name

users
  id
  name

This is the code for the form

<%= form_for @event do |f| %>
  <%= f.fields_for :users, f.object.users do |builder| %>
    <%= builder.text_field :name, "Name" %>
    <%= f.fields_for :event_users do |builder2| %>
      <%= builder2.hidden_field :is_participating, :value => true %>
    <% end %>
  <% end %>
<% end %>

What I'm trying to accomplish is to set the field is_participating in the events_users table, but it doesn't work!

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

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

发布评论

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

评论(2

贵在坚持 2024-10-18 09:14:14

我遇到了同样的问题,基本上我所做的如下:

<%= form_for @event do |f| %>
   <%= f.fields_for :event_users, @event_user do |builder| %>
      <%= builder.hidden_field :is_participating, :value => true %>
      <%= f.fields_for :users, f.object.user do |builder2| %>
         <%= builder2.text_field :name, "Name" %>
      <% end %>
   <% end %>
<% end %>

即,我切换了嵌套对象的顺序。这要求您通过声明的连接表建立关系(您确实这样做了)。它为我保存了三个表中的所有属性。

但是,如果您想保留当前的模型,我想知道您是否不应该为第二级嵌套表单设置与第一级相同的设置。也就是说,您的 f.fields_for :event_users 后面应该跟一个逗号和该类的实例。

I had this same problem and essentially what I did was the following:

<%= form_for @event do |f| %>
   <%= f.fields_for :event_users, @event_user do |builder| %>
      <%= builder.hidden_field :is_participating, :value => true %>
      <%= f.fields_for :users, f.object.user do |builder2| %>
         <%= builder2.text_field :name, "Name" %>
      <% end %>
   <% end %>
<% end %>

i.e., I switched the order of the nested objects. This requires that you have a relationship through your join table declared (which you do). It saved all the attributes across the three tables for me.

If you want to keep your current model, however, I wonder if you shouldn't have the same set up for the 2nd level nested form as the first. Namely, your f.fields_for :event_users should be followed by a comma and an instance of that class.

屋顶上的小猫咪 2024-10-18 09:14:14

您的 events_users 表没有“id”字段作为主键吗?如果您发布 events_users 联接表的表布局可能会有所帮助。

Does your events_users table not have an 'id' field as a primary key? Might help if you posted the table layout for your events_users join table.

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