Search code examples
ruby-on-rails-3scopemass-assignmentscoped-mass-assignment

Scoped mass assignment and accepts_nested_attributes_for in Rails 3.1 not working?


Using Rails 3.1 RC4.

My User model has the following:

  has_many :emails, :dependent => :destroy
  accepts_nested_attributes_for :emails

My Email model has the following:

  belongs_to :user
  attr_accessible :email, :email_confirmation, :as => :admin

In Rails console:

User.first.update_attributes!({:artist_name => 'foo', :emails_attributes => {0 => {:email => 'foo@blah.com', :email_confirmation => 'foo@foo.com'}}}, :as => :admin)

I get:

WARNING: Can't mass-assign protected attributes: email, email_confirmation

In my Email model, if I remove :as => :admin. Everything works...

Should I be assigning some kind of scope to accepts_nested_attributes_for? Anyone know how this can be fixed?


Solution

  • Issue and solution has been highlighted here.

    In summary, an options hash must be passed.