Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

attr_accessible in rails Active Record

When I use the attr_accessible to specify which fields from my Model I will expose, is it true for script/console as well? I mean something that I didn't specify as attr_accessible won't be accessible as well through console ?

like image 538
VP. Avatar asked Nov 24 '09 13:11

VP.


2 Answers

This is only true for mass assignment. For instance, if you were to set attr_protected :protected in your model:

>> Person.new(:protected => "test")
=> #<Person protected: nil>

Conversely, you could set all attributes you want as accessible using attr_accessible.

However, the following will still work:

>> person = Person.new
=> #<Person protected: nil>
>> person.protected = "test"
=> #<Person protected: "test">

This is the same behaviour as in controllers, views, etc. attr_protected only protects against mass assignment of variables, primarily from forms, etc.

like image 62
Josh Delsman Avatar answered Sep 21 '22 17:09

Josh Delsman


The console behaves exactly as your Rails application. If you protected some attributes for a specific model, you won't be able to mass assign these attributes either from console or from the Rails app itself.

like image 42
Simone Carletti Avatar answered Sep 24 '22 17:09

Simone Carletti