Search code examples
ruby-on-rails-3.2rails-migrationsmass-assignment

How to mass-assign protected attributes in a migration without changing the model


I'm using Rails 3.2

Is there a special command to assign a protected attribute in a migration without modifying the model with a "attr_accessible" statement.

I do have an attribute "pub_convention_id" that must stay protected in production mode.

However, I need to update its value during a migration. That's why I'm geting this "famous message" : Can't mass-assign protected attributes: pub_convention_id

So I'm asking : is there a special statement to temporary deactivate the mass assignement protection (in a block for example) ?

This is the concerned part of my migration file :

ProjEncaissementCofin.all.each do |proj_encaissement_cofin|
  proj_encaissement_cofin.update_attributes! :pub_convention_id => 1
end

Thanks for your help


Solution

  • proj_encaissement_cofin.pub_convention_id = 1
    proj_encaissement_cofin.save!
    

    or

    proj_encaissement_cofin.assign_attributes({ pub_contention_id: 1 }, without_protection: true)
    proj_encaissement_cofin.save!