I cannot really find a clean way to bypass attr_accessible when creating seed data. I would like to be able to use mass assignment without any problems, since i know that this seed file is totally safe.
Is there a clean way to force Rails 3 to accept those ?
Here's a quick hack (thanks Mike), put this at the top of your seeds.rb:
# Dodge the mass assignment
User.send(:attr_accessible, :username)
User.send(:attr_accessible, :admin)
Now you can call this with ease without cluttering up your model (using :as => :seed):
@user = User.find_or_create_by_username(:username => 'ryanonrails', :admin => true)
#in model
attr_accessible :name, :role,.... :as => :seed
#in seed.rb
model.assign_attributes({name: "Putin", role: "president"....},:as => :seed)
model.save
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With