Load and use fixture in rails console

28,047

Solution 1

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'.

Solution 2

May be late... Rails 4

require 'active_record/fixtures'
ActiveRecord::FixtureSet.create_fixtures(Rails.root.join('test', 'fixtures'), 'users')

Solution 3

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')

Solution 4

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.

Solution 5

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
Share:
28,047

Related videos on Youtube

Patrick Pruneau
Author by

Patrick Pruneau

Always in search of better knowledge and working method!

Updated on November 17, 2020

Comments

  • Patrick Pruneau
    Patrick Pruneau over 3 years

    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 :)

  • Tiemen
    Tiemen almost 12 years
    Thanks for that answer - however (in Rails 3.2.5, at least) the command is: rails c test
  • Rob
    Rob over 8 years
    hmm ... that didn't work for me, but this does: rake db:fixtures:load RAILS_ENV=test FIXTURES_PATH='spec/fixtures' FIXTURES=users,participants
  • Masa Sakano
    Masa Sakano over 3 years
    As of Rails 6.0, rails c test does not seem to work. Do instead RAILS_ENV=test rails c
  • Ryan Sandridge
    Ryan Sandridge over 3 years
    @MasaSakano LOL... back to my original answer 😂
  • James H
    James H almost 3 years
    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.