Is there a way to tell Rails to ignore attr_accessible when seeding the database and to allow mass assignment with create or new? I know how to skip validations but do not find anything on mass assignment…
Markus Proske wrote in post #979510:
Is there a way to tell Rails to ignore attr_accessible when seeding the database and to allow mass assignment with create or new? I know how to skip validations but do not find anything on mass assignment...
Here's one way I found a quick Google search:
Thank you. I’ve already been there and this works but I was looking for a more global setting for all entries in the seed file. Is there really no other way?
Markus
Bit hacky but you could override remove_attributes_protected_from_mass_assignment or overwrite the attribute containing the list of protected attributes
Fred
Bit hacky but you could override
remove_attributes_protected_from_mass_assignment or overwrite the
attribute containing the list of protected attributes
Thank you! I did not find the remove_attributes_protected…, but did find an interesting method sanitize here: rails / activemodel / lib / active_model / mass_assignment_security / sanitizer.rb
My solution for the archives: seeds.rb module ActiveModel module MassAssignmentSecurity module Sanitizer def sanitize(attributes) attributes end end end end
Just to save the next person the aggravation of figuring this out, Sanitizer is a class now, not a module, and the sanitize method takes two parameters. So you need this:
module ActiveModel module MassAssignmentSecurity class Sanitizer def sanitize(attributes, authorizer) attributes end end end end