I need to fill the test development database with data, for example from factorygirl
, but I'd like to use it from rails console.
How I put example data in db so I can fetch it from console and do some test there?

- 3,272
- 2
- 25
- 34
-
Check http://stackoverflow.com/questions/1050047/how-to-run-rails-console-in-the-test-environment-and-load-test-helper-rb . Hope it will help you – Sergey Kishenin Mar 17 '11 at 18:32
-
why do you need this in the test database? wouldnt just having it in development work for you? typically the test database gets reset and recreated so much that theres no point to prefilling it with any significant amounts of data – Will Ayd Mar 17 '11 at 18:33
-
Edited my question, sure I mean development DB. – methyl Mar 17 '11 at 18:45
4 Answers
Faker is also a good solution.
Here's how my lib/tasks/sample_data.rake
looks like. I run it with rake db:populate
.
Creates 50 entries with random info.
require 'faker'
namespace :db do
desc "Fill database with sample data"
task :populate => :environment do
Rake::Task['db:reset'].invoke
50.times do |n|
name = Faker::Company.name
year = 1900+rand(111)
rating = 1+rand(10)
watched = (1 == rand(2) ? true : false)
imdb_id = rand(1000000)
Movie.create!(:name => name,
:year => year,
:rating => rating,
:watched => watched,
:imdb_id => imdb_id)
end
end
end

- 444
- 3
- 23

- 646
- 3
- 17
-
6It should be `lib/tasks/sample_data.rake`... File suffix is wrong and rake could not find this task! – zhongxiao37 Mar 30 '12 at 08:45
I've made a gem test_dummy that works like Factory Girl to define lots of fake data. When properly configured you can do things like this:
# Create 100 fake companies
100.times { Company.create_dummy }
# Create a single fake company on-demand
fake_company = Company.create_dummy
The alternative is to use the db/seeds.rb
facility or to load in your fixtures into your development environment.

- 208,517
- 23
- 234
- 262
-
-
@methyl `seeds.rb` is for real data that your application needs in order to function. For example, a list of zip codes, or states, or countries, or tax rules, or whatever. – iconoclast Mar 21 '18 at 18:49
-
@iconoclast Replying almost 7 years after the fact to the day is better late than never, huh? – tadman Mar 21 '18 at 18:50
-
2@tadman: interesting timing... but I've never considered StackOverflow questions to become irrelevant simply based on the passage of time—only when technology changes make the question irrelevant—so I'd say "better *ever* than *never*" since there's nothing making my comment "late" (as though I was supposed to write it 7 years ago or something ) – iconoclast Mar 21 '18 at 22:12
-
@iconoclast Just half joking. Additional explanation is always appreciated as it might help someone else. – tadman Mar 21 '18 at 22:14
Michael Hartl provides an excellent introduction to this topic as part of the railstutorial.org program.
He uses a gem called Factory Girl, which is designed to ease the process of populating a database with sample data.
E.G.
http://ruby.railstutorial.org/chapters/user-microposts#sec:sample_microposts
https://github.com/railstutorial/sample_app/blob/master/lib/tasks/sample_data.rake

- 785
- 5
- 20
Is it just in the Rails console or just 'from the console'?
I like to use a Thor or Rake task to do that. Instead of Factory Girl I use Machinist.
You may want to check this answer
Rails: Good Rspec2 example usage? (Also: Cucumber, Pickle, Capybara)