Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing created_at for seed.rb data

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
like image 213
Chap Avatar asked Mar 10 '10 13:03

Chap


2 Answers

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.

like image 88
shingara Avatar answered Oct 06 '22 02:10

shingara


This works:

p = Project.create :name => 'Dummy Project'
p.update_attribute :created_at, (rand*10).days.ago
like image 22
Chap Avatar answered Oct 06 '22 01:10

Chap