Your rails app has a special folder where you put "tasks" to be run. This folder is...
Most people have a task that they use to populate their database with "test data". This powerful feat can be accomplished with the simple command...
But for it to work, you of course need to first define the task "db:populate". Here's a sample definition you're free to look over.
(lib/tasks/sample_data.rake)
In that task, we first create an admin user, then we make 99 other users. It's very handy for testing with... As your application undergoes changes, you'll likely be watching this file closely and keeping it up to date as you're testing new functionality.
Never Forget
lib/tasks/
Most people have a task that they use to populate their database with "test data". This powerful feat can be accomplished with the simple command...
$ rake db:populate
But for it to work, you of course need to first define the task "db:populate". Here's a sample definition you're free to look over.
(lib/tasks/sample_data.rake)
namespace :db do
desc "Fill database with sample data"
task :populate => :environment do
Rake::Task['db:reset'].invoke
make_users
end
end
def make_users
admin = User.create!(:email => "[email protected]",
:password => "password",
:password_confirmation => "password")
admin.toggle!(:admin)
admin.persons.create(:first_name => "Firstname", :last_name => "LastName", :email => admin.email)
99.times do |n|
name = Faker::Name.name
email = "example-#{n+1}@railstutorial.org"
password = "password"
User.create!(:email => email,
:password => password,
:password_confirmation => password).persons.create(:first_name => name, :last_name => name, :email => email)
end
end
In that task, we first create an admin user, then we make 99 other users. It's very handy for testing with... As your application undergoes changes, you'll likely be watching this file closely and keeping it up to date as you're testing new functionality.
Never Forget
$ rake db:populate
0 Comments On This Entry
Trackbacks for this entry [ Trackback URL ]
← January 2022 →
| S | M | T | W | T | F | S |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| 9 | 10 | 11 | 12 | 13 | 14 | 15 |
| 16 | 17 | 18 | 19 | 20 | 21 | 22 |
| 23 | 24 | 25 | 26 | 27 | 28 | 29 |
| 30 | 31 |
Tags
My Blog Links
Recent Entries
-
-
Rails - Sending delete signals to rails with jQuery since it broke PROTOTYPE's delete linkson Feb 25 2012 08:56 AM
-
Rails - How to backup and restore the current status of the database easilyon Jan 28 2012 11:14 AM
-
Rails: Populate your database with sample dataon Oct 28 2011 04:25 PM
-
Web Related - Most Potent Librarieson Sep 06 2011 02:32 PM
Recent Comments
-
NotarySojac on Aug 03 2011 02:50 PM
005 Rails: From Views to Models -- The flow of data from the user
Search My Blog
3 user(s) viewing
3 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)



Leave Comment









|