Tuesday, August 7, 2012

Bypass Mass-Assignment Protection In Rake db:seed

Mass-assignment protection is crucial to Rails security, but it's just an annoyance when you're setting up default dev DB contents with rake db:seed.

This solution might not be the best, but it's very simple: since attr_accessible is just a class method, you can use it anywhere. So use it inside your db/seeds.rb, e.g.:

Model.attr_accessible :normally_inaccessible_attribute

It won't affect anything else in your app, and it'll make setting up your defaults less of a hassle.