Bypassing mass-assignment protection in rake db:seed

2012-12-19

Reference: GilesBowkett

So I ran this problem the other day on a project where the db/seed.rb file couldn’t run due to mass-assignment protection. Instead of adding the attribute in attr_accessible on the model side and then removing it after running the seed data, there’s actually another way to do it.

Giles suggested adding attr_accessible directly into the seed file itself, such that it will temporarily disable mass-assignment protection for the duration of the rake db:seed. Like this:

# db/seed.rb
Model.attr_accessible :normally_inaccessible_attribute

Now this works like a charm and allows your model to be kept in its pristine state, while not adding further complexity and opportunities for developer block, when new team members come in.

I’m currently working on fyra.sh, a CLI-first static site deployment tool where you push your site and it’s served globally through a built-in CDN, without the overhead of heavy platforms.