Rails,使用 has_many through 和 fields_for
我在让它发挥作用时遇到了一些麻烦。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,基本上我所做的如下:
即,我切换了嵌套对象的顺序。这要求您通过声明的连接表建立关系(您确实这样做了)。它为我保存了三个表中的所有属性。
但是,如果您想保留当前的模型,我想知道您是否不应该为第二级嵌套表单设置与第一级相同的设置。也就是说,您的 f.fields_for :event_users 后面应该跟一个逗号和该类的实例。
I had this same problem and essentially what I did was the following:
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.
您的 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.