Create a script or task to modify database
I need to create 开发者_StackOverflow中文版a script that imports data from a file system source. How do I do that?
I already tried to create a rake task but there the models are not loaded. How do I get the whole rails environment into my task?
desc 'Do stuff with models'
task :do_stuff => :environment do
1000.times.each do |i|
Model.create :name => "model number #{i}"
end
end
You declare :environment
as a dependency of your rake task. This loads up rails and all of your app code before it runs.
精彩评论