4

How does Rails 3.1 (RC4) and scoped mass assignment expect us to work with seeds.rb when loading a list of data.

For example. I normally have something like:

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

Which creates over 100+ cities. this doesn't work anymore since the City model has a scoped mass assignment :as => :admin.

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

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

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

Any thoughts on this?

Joerg
  • 3,553
  • 4
  • 32
  • 41
Christian Fazzini
  • 19,613
  • 21
  • 110
  • 215

1 Answers1

12

You can do the following:

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

This completely overrides the mass assignment protection - so be sure to only use this in say the seeds.

Joerg
  • 3,553
  • 4
  • 32
  • 41