Ignore attr_accessible in seed file

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 P. 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:

On Thursday, February 3, 2011 11:58:23 PM UTC+1, Ruby-Forum.com User
wrote:

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

On Feb 4, 8:51am, Markus P. [email protected] wrote:

On Thursday, February 3, 2011 11:58:23 PM UTC+1, Ruby-Forum.com User wrote:

Here’s one way I found a quick Google search:

database - How to avoid the validation, callbacks and 'attr_accessible' effects during the seeding process using Ruby on Rails 3? - Stack Overflow

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?

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:
railshttps://github.com/rails/rails/tree/59f7780a3454a14054d1d33d9b6e31192ab2e58b/
activemodelhttps://github.com/rails/rails/tree/59f7780a3454a14054d1d33d9b6e31192ab2e58b/activemodel/
libhttps://github.com/rails/rails/tree/59f7780a3454a14054d1d33d9b6e31192ab2e58b/activemodel/lib/
active_modelhttps://github.com/rails/rails/tree/59f7780a3454a14054d1d33d9b6e31192ab2e58b/activemodel/lib/active_model/
mass_assignment_securityhttps://github.com/rails/rails/tree/59f7780a3454a14054d1d33d9b6e31192ab2e58b/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