Load and use fixture in rails console
Asked Answered
P

6

51

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

Prig answered 12/8, 2011 at 17:42 Comment(0)
S
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'.

Spelaean answered 26/10, 2011 at 16:40 Comment(3)
Thanks for that answer - however (in Rails 3.2.5, at least) the command is: rails c test – Pathless
As of Rails 6.0, rails c test does not seem to work. Do instead RAILS_ENV=test rails c – Mastrianni
@MasaSakano LOL... back to my original answer πŸ˜‚ – Spelaean
P
40

May be late... Rails 4

require 'active_record/fixtures'
ActiveRecord::FixtureSet.create_fixtures(Rails.root.join('test', 'fixtures'), 'users')
Phira answered 22/5, 2013 at 5:39 Comment(0)
A
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')
Algophobia answered 8/9, 2013 at 14:15 Comment(0)
K
8

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.

Kenton answered 18/8, 2014 at 8:44 Comment(0)
J
7

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
Jovian answered 26/8, 2015 at 22:10 Comment(2)
hmm ... that didn't work for me, but this does: rake db:fixtures:load RAILS_ENV=test FIXTURES_PATH='spec/fixtures' FIXTURES=users,participants – Cattier
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. – Lettie
B
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
Bo answered 29/8, 2015 at 23:24 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.