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.cr开发者_如何学Goeate :name => 'Dummy Project',
:created_at => Date.today - rand(10).days
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.
This works:
p = Project.create :name => 'Dummy Project'
p.update_attribute :created_at, (rand*10).days.ago
精彩评论