David - Musings of an SRE

Bypassing mass-assignment protection in rake db:seed

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.