Joel, (always one syllable), often "@jayroh"

menu

From the Ruby Noob Dept: Issue(s) with accepts_nested_attributes_for

27 Feb 2010

Finally took some time to jump in and refactor some nested forms at Thredded using Rails 2.3′s accepts_nested_attributes_for. Thanks to Ryan Bates’ screencasts on the topic it was fairly easy. A little code cleanup and everything worked as it should … other than one thing.

Can’t mass-assign these protected attributes

Noticed that error in my development logfiles after a particular form wasn’t being saved. A User class had some protected, and some not, attributes and until I added the Profile attributes to it, the nested form submitting a User and it’s associated Profile record would not save.

class User < ActiveRecord::Base
  has_one  :profile
  accepts_nested_attributes_for :profile
  attr_accessible :login, :email, :password, :password_confirmation
  # ...
end

Needed just the Profile attributes set as accessible and ready for mass assignment

class User < ActiveRecord::Base
  has_one  :profile
  accepts_nested_attributes_for :profile
  attr_accessible :login, :email, :password, :password_confirmation, :profile_attributes
  # ...
end

Another case where you need to know at all times where and if properties of your classes are locked down or not.

close
Joel Oliveira

Hi, I'm Joel.

An engineering manager in Boston. Lapsed "tweeter" at @jayroh. Thinking about family, my (bull)dogs, code, comics, and building things like shubox.

Articles from 2012

Articles from 2007