How to use factory_girl for debug data - ruby-on-rails

I use Rails4.
fixtures
I use fixtures for debug data.
but
$ bin/rake db:fixtures:load FIXTURES_PATH=spec/fixtures
not run before_validation and other callbacks.
factory_girl
I use factory_girl for test.
like
$ bundle exec rspec spec/models/foo_spec.rb.
seed
I know how to use factory_girl in seed
$ bundle exec rake db:seed
but I want to use seed for only master data.
Question
How to use factory_girl for debug data. and Do I use what command?(rake ?? or spec or something else?)

Make a rake task that uses your factories to create your debug data. For example, if you had a model (and corresponding factory) named Report:
namespace :db do
desc "Fill database with debug data"
task :debug_data => :environment do
puts "Destroy existing data?"
if STDIN.gets.chomp.upcase == 'Y'
if Rails.env.production?
raise "\nI'm sorry, Dave, I can't do that.\n(You're asking me to drop your production database.)"
end
Report.destroy_all
end
FactoryGirl.create(:report, name: 'Fred')
FactoryGirl.create(:report, name: 'Barney')
end
Place this file in: lib/tasks/debug_data.rake
Execute it using:
bundle exec rake db:debug_data

Related

Don't know how to build task 'db:seed_fu'

I have just added the seed-fu gem to my app for seeding my test-database:
group :test do
gem 'seed-fu'
end
I made a custom rake task (in /lib/tasks/db.rake) for seeding only my test-database:
namespace :db do
desc "seed_fu only in test-database"
task seed_fu_test: :environment do
Rails.env = 'test'
puts "Seeding will be made in test-base ONLY!"
Rake::Task["db:seed_fu"].execute
end
end
If I do rake -T | grep seed then my new custom-made task is shown amongst other seed-tasks:
rake db:seed # Load the seed data from db/seeds.rb
rake db:seed_fu # Loads seed data for the current environment
rake db:seed_fu_test # seed_fu only in test-database
Now when I do rake db:seed_fu_test I get
rake aborted!
Don't know how to build task 'db:seed_fu'
But when I do
rake db:seed_fu RAILS_ENV='test'
then seed_fu seeds my test-database well.
Figured it out- the problem was in my Gemfile. Because I added the seed-fu gem into test-group then in development-environment, which was my default for running also the rake db:seed_fu_test task, the seed_fu gem was not seen.
Therefore when moving gem 'seed-fu' line into my :development-group in Gemfile, the problem was solved.

How to fix "uninitialized constant" in a Rake task

I have a problem when I do:
namespace :xaaron do
task :get_roles do
roles = Xaaron::Role.all
puts roles
end
task :get_role, [:name] do |t, args|
role = Xaaron::Role.find(args[:name].parameterize)
puts role
end
end
The first task will work fine. I can even add binding.pry and run Xaaron::Role and get information about Roles back. But the second task fails with:
NameError: uninitialized constant Xaaron::Role
I run each task in my main app because these tasks are inside an engine, using:
bin/rake xaaron:get_roles` and `bin/rake xaaron:get_role
I can run bin/rails c in the main application that uses the engine and run Xaaron::Role and get information about Roles table.
Why is the second one failing but the first one is not? Is there scoping with arguments?
I'm not sure why either works, but if this is Rails and those are Rails models, your tasks should depend on the environment:
task :get_roles => [ :environment ] do
By depending on the :environment task, it first loads Rails.
Also see: What's the 'environment' task in Rake?.
You can also run a Rake task as
bundle exec rake environment xaaron:get_role
This will load the Rails environment first.
I kept getting uninitialized constant errors for a Rake task, even after depending on :environment and running with bundle exec.
The issue was that I was making a Rake::TestTask and, even though the Rake task had access to all constants, the test files themselves did not have access to constants.
The solution was to add this line to the top of my test file:
require_relative '../config/environment'
This is the Rake task:
require "rake/testtask"
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/test_*.rb"]
end
To add, as of Ruby 1.9 and above, you can use this hash syntax:
namespace :xaaron do
desc "Rake task to get roles"
task get_roles: :environment do
roles = Xaaron::Role.all
puts roles
end
#####
end
And then you can run the command below to run the Rake task:
rake xaaron:get_roles
or
bundle exec rake xaaron:get_roles

Ruby Script to create new model value

Project is the model name and I want to do something like:
Project.create(:name => 'projectname', :identifier => 'projectidentifier')
This should be done in the terminal through a ruby script. I am not going to use rails console to create it nor use seeds.rb in a db file to migrate this as rake db:seed.
Can someone help. Thanks
The easiest way would be using rails runner (which essentially loads rails):
rails runner your_script.rb
The line of code would be a content of that script.
What about a rake task?
on lib/tasks, create a file named data.rake, and the content:
namespace :data
desc "Create project data"
task create_project_data: :environment do
Project.create(name: 'projectname', identifier: 'projectidentifier')
end
end
And you can run it as any rake task
rake data:create_project_data
And it will also appear when you list your rake tasks
rake -T

Rails Rake Task - Access to model class

I would like to define a Ruby (1.9.2)-on-Rails(3.0.5) rake task which adds a user to the User table. The file looks like this:
#lib/tasks/defaultuser.rake
require 'rake'
namespace :defaultuser do
task :adduser do
u=User.new
u.email="bob#example.com"
u.password="password"
u.save
u.errors.each{|e| p e}
end
end
I would then invoke the task as
> rake defaultuser:adduser
I tested the code in the :adduser task in the Rails console, and it works fine.
I tested the rake task, running only
print "defaultuser:adduser"
in the body of the task, and it worked fine.
However, when I combined them, it complained, saying
rake aborted!
uninitialized constant User
I tried a
require File.expand_path('../../../app/models/user.rb', __FILE__)
at above the namespace definition in the rake file, but that didn't work. I got
rake aborted!
ActiveRecord::ConnectionNotEstablished
What do I need to do so that I have the same access to the User model class in the Rake task that I have in the Rails console?
You're close :)
#lib/tasks/defaultuser.rake
require 'rake'
namespace :defaultuser do
task :adduser => :environment do
...
end
Note the use of :environment, which sets up the necessary Rails environment prior to calling the rake task. After that, your User object will be in scope.

'rake' "in order to" 'rake'

To prepare database for my Ruby on Rails 3 application I need to run the following steps in the Terminal:
rake db:create
rake db:migrate
rake db:seed
Is it possible to do all those steps in one? Maybe it is possible running a 'rake' command that will "fire" another 'rake' command... but how?!
You can define your own rake tasks which call other tasks as prerequisites:
# lib/tasks/my_tasks.rake
namespace :db do
desc "create, migrate and seed"
task :do_all => [:create,:migrate,:seed] do
end
end
Normally the body of the task would contain Ruby code to do something, but in this case we are just invoking the three prerequisite tasks in turn (db:create,db:migrate,db:seed).
The empty do-end blocks are not needed, e.g. (for zetetic's answer)
$ cat lib/tasks/my_tasks.rake
# lib/tasks/my_tasks.rake
namespace :db do
desc "create, migrate and seed"
task :do_all => [:create,:migrate,:seed]
end
rake db:create db:migrate db:seed will do all that.
zeteitic got it right, but in the event you don't want to namespace this task under "db", you'd want something more like this:
desc "Bootstrap database."
task :bootstrap => ["db:create", "db:migrate", "db:seed"] do; end
And on the command line:
rake bootstrap
# => create, migrate and seed db

Resources