-->

Load and use fixture in rails console

2020-05-15 12:44发布

问题:

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

回答1:

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

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

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



回答2:

May be late... Rails 4

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


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


回答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.



回答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


回答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