How to Inoculate a Rails 3.1 Distributed Bulk Application

How Rails 3.1 (RC4) and bulk assignment with extended range expect us to work with seeds.rb when loading a list of data.

For instance. I usually have something like:

City.create([ { :name => 'Chicago' }, { :name => 'Copenhagen' }, ... ]) 

Which creates more than 100 cities. this does not work anymore, since the city model has a limited mass assignment :as => :admin .

As far as I know, the .create() method does not allow us to throw :as => :admin . Only .new() and .update_attributes() allow us to do this with :as => :admin .

So doing something like (below) is cumbersome (especially for 100+ entries):

 city1 = City.new({ :name => 'Chicago' }, :as => :admin) city1.save city2 = City.new({ :name => 'Copenhagen' }, :as => :admin) city2.save 

Any thoughts on this?

+4
source share
1 answer

You can do the following:

 City.create([ { :name => 'Chicago' }, { :name => 'Copenhagen' }, ... ], :without_protection => true) 

This completely removes the protection of mass assignment - so be sure to use it only in seeds.

+12
source

All Articles