views:

127

answers:

2

I'm using seeds.rb to load some dummy data into my project as I develop it.

I'd like to use a random created_at date for my records, but the created_at date is always set to Time.now on create.

  #seeds.rb
  Project.create :name => 'Dummy Project',
                 :created_at => Date.today - rand(10).days
+1  A: 

This works:

p = Project.create :name => 'Dummy Project'
p.update_attribute :created_at, (rand*10).days.ago
chap
+4  A: 
project = Project.create(:name => 'Dummy Project')
project.created_at = (rand*10).days.ago
project.save

The created_at can't be defined during creation. It can only be changed afterwards.

shingara
+1. PS: Fixed the syntax error.
KandadaBoggu