Bypassing attr_accessible in seeds.rb

I cannot find a clean way to get around attr_accessible when creating seed data. I would like to be able to use bulk assignment without any problems, since I know that this seed file is absolutely safe.

Is there a clean way to get Rails 3 to accept them?

+4
source share
2 answers
#in model attr_accessible :name, :role,.... :as => :seed #in seed.rb model.assign_attributes({name: "Putin", role: "president"....},:as => :seed) model.save 
+5
source

Here's a quick hack (thanks to 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 easily call it without cluttering your model (using: as =>: seed):

 @user = User.find_or_create_by_username(:username => 'ryanonrails', :admin => true) 
+8
source

Source: https://habr.com/ru/post/1413354/


All Articles