Wednesday, 25 April 2012

attr_accessor and attr_accessible in Rails

While trying to figure out why I was getting this error in a Rails application:
 WARNING: Can't mass-assign protected attributes  

I realised I'd confused the two below terms in my Rails model:

 attr_accessor  
is built into Ruby and defines a getter and setter method for a class attribute.


 attr_accessible  
is defined by Rails and makes the attribute mass-assignable.

Generally, in a Rails application, it's attr_accessible we want to be using.

No comments:

Post a Comment

Please leave a comment if you find this blog helpful or interesting! I'd love to hear from you!