我很难让它工作。
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 ,但它不起作用!