我有以下型号:
class EventParticipant < ActiveRecord::Base
belongs_to :event
has_one :user
attr_accessible :name, :email, :is_organizer, :is_attending
end
class Event < ActiveRecord::Base
has_many :event_participants
attr_accessible :event_participants_attributes
accepts_nested_attributes_for :event_participants
end
class HappyHour < Event
end
及其相应的架构:
create_table "event_participants", :force => true do |t|
t.string "name"
t.string "email"
t.boolean "is_organizer"
t.boolean "is_attending"
t.integer "event_id"
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "events", :force => true do |t|
t.string "name"
t.boolean "is_finalized"
t.string "type"
t.datetime "created_at"
t.datetime "updated_at"
end
add_foreign_key "event_participants", "events", :name => "event_participants_events_fk"
现在,在EventParticipantsController控制器上,我从请求中加载HappyHour模型,如下所示:
@happy_hour = HappyHour.new(params[:happy_hour])
但由于某些原因,HappyHour的event_participants属性的批量分配失败:
在2012-01-25 18:55:31 -0300为127.0.0.1开始POST“/ event_participants” 由EventParticipantsController处理#create为JS 参数:{“happy_hour”=&gt; {“event_participants”=&gt; {“0”=&gt; {“name”=&gt;“Diego”,“email”=&gt;“XXX@YYY.com”,“is_organizer “=&gt;”true“},”1“=&gt; {”name“=&gt;”测试“,”电子邮件“=&gt;”test@email.com“,”is_organizer“=&gt;”false“} },“add_event_participant”=&gt;“aaa@bbb.com”} 警告:无法批量分配受保护的属性:event_participants
我错过了什么?我已经找到了很多关于在一对一关系中对相关对象进行质量分配的例子,但是没有像我的情况那样只有一对多的例子。