50

I wonder if there's a way to load and/or use fixture in rails console. Actually, I'd like to create a user from my fixture users.yml to do some testing without having to go through all the "pain" of doing User.new(:name = "John", :email = "..") each time.
I am currently in test environment (rails c RAILS_ENV=test).

If it's not a good way to do things, please say it. I'm new to Rails so I'm here to learn :)

Ryan
  • 2,073
  • 1
  • 19
  • 33
Patrick Pruneau
  • 669
  • 3
  • 7
  • 14

6 Answers6

56

You should be able to load your fixtures prior to entering console. Like this:

RAILS_ENV=test bin/rails db:fixtures:load
RAILS_ENV=test bin/rails console

However, you still won't be able to access your fixture data like you would in a test. This simply loads your test database with your fixtures data. So you'd still have to do something like:

user = User.find_by(name: "John")

But, you can still create shortcuts for this sort of thing. You can add any ruby code you'd like to your ~/.irbrc. I suggest creating a .railsrc file as described here. You can then set up things like:

john = User.find_by(name: "John")

So now you can just start referring to the variable 'john' after console loads. Incidentally, the post I linked to shows how to set up a global .railsrc file, but you could set it up so that you had a per project .railsrc. Or, if you want something a little less fancy, but easy to do... just create a ruby file in your project (maybe 'shortcuts.rb'). After console is loaded, just do a require 'shortcuts'.

Ryan
  • 2,073
  • 1
  • 19
  • 33
39

May be late... Rails 4

require 'active_record/fixtures'
ActiveRecord::FixtureSet.create_fixtures(Rails.root.join('test', 'fixtures'), 'users')
woto
  • 2,893
  • 1
  • 29
  • 24
18

You can load fixtures into your development database too:

$ rake db:fixtures:load
$ rails c
> require 'active_record/fixtures'
> john = User.find ActiveRecord::FixtureSet.identify('john')
Wojtek Kruszewski
  • 13,940
  • 6
  • 38
  • 38
7

So I had a similar but slightly different need. I wanted to use my existing fixtures (from my rspec test) to populate my development database. This is how I did it by adding a new task to my rake file (located in libs/tasks/*.rake):

task d_populate: :environment do
  require 'active_record/fixtures'
  fixtures_dir = File.join(Rails.root, '/spec/fixtures') #change '/spec/fixtures' to match your fixtures location
  Dir.glob(File.join(fixtures_dir,'*.yml')).each do |file|
  base_name = File.basename(file, '.*')
  puts "Loading #{base_name}..."
  ActiveRecord::Fixtures.create_fixtures(fixtures_dir, base_name)
  end
end

If you combine this with a db:reset you can populate your development environment at will by adding this to your rake task as well:

task reseed: [:environment, 'db:reset', 'db:d_populate']

Then you can call rake db:reseed to populate from fixture YAML files.

hercules08
  • 119
  • 2
  • 3
6

It's possible to specify an alternate fixture directory using the FIXTURES_DIR variable. The value should be relative to test/fixtures.

$ rake db:fixtures:load RAILS_ENV=test FIXTURES_DIR='../../spec/fixtures'

It's also possible to specify a limited set of fixtures

$ rake db:fixtures:load RAILS_ENV=test FIXTURES_DIR='../../spec/fixtures' FIXTURES=users,user_roles
Fred Willmore
  • 4,386
  • 1
  • 27
  • 36
  • 7
    hmm ... that didn't work for me, but this does: rake db:fixtures:load RAILS_ENV=test FIXTURES_PATH='spec/fixtures' FIXTURES=users,participants – Rob Sep 17 '15 at 18:49
  • 1
    This is the most up to date way to do it as of Rails 6. `FIXTURES_DIR` may be used to specify a sub directory of test/fixtures. `FIXTURES_PATH` may be used to specify any path. – James H Jul 16 '21 at 18:46
6

You can load a fixture in the Rails 3.2 console as follows:

require 'active_record/fixtures'

ActiveRecord::Fixtures.create_fixtures FIXTURE_PATH_HERE, MODEL_NAME_HERE
jpw
  • 18,697
  • 25
  • 111
  • 187