I want to populate a new feature with dummy data, but don't want to use the db/seeds.rb file as it already has seeds other data irrelevant for this feature.
To run the default seeds.rb file, you run the command rake db:seed.
If I create a file in the db directory called seeds_feature_x.rb, what would the rake command look like to run (only) that file?
Start by creating a separate directory to hold your custom seeds – this example uses db/seeds. Then, create a custom task by adding a rakefile to your lib/tasks directory:
# lib/tasks/custom_seed.rake
namespace :db do
namespace :seed do
Dir[Rails.root.join('db', 'seeds', '*.rb')].each do |filename|
task_name = File.basename(filename, '.rb')
desc "Seed " + task_name + ", based on the file with the same name in `db/seeds/*.rb`"
task task_name.to_sym => :environment do
load(filename) if File.exist?(filename)
end
end
end
end
This rakefile accepts the name of a seed file in the db/seeds directory (excluding the .rb extension), then runs it as it would run seeds.rb. You can execute the rake task by issuing the following from the command line:
rake db:seed:file_name # Name of the file EXCLUDING the .rb extension
Update: Now it should also list the seed tasks when running rake --tasks or rake -T.
I tried out zeantsoi's answer but it didn't give me what I wanted, it did all files in a directory. Hacked away at it and got this.
namespace :db do
namespace :seed do
task :single => :environment do
filename = Dir[File.join(Rails.root, 'db', 'seeds', "#{ENV['SEED']}.seeds.rb")][0]
puts "Seeding #{filename}..."
load(filename) if File.exist?(filename)
end
end
end
And to use this do the following:
rake db:seed:single SEED=<seed_name_without_.seeds.rb>
This will look in the Rails.root/db/seeds folder for a file name without the .seeds.rb (it adds those for you).
Working example:
rake db:seed:single SEED=my_custom_seed
The above would seed the Rails.root/db/seeds/my_custom_seed.seeds.rb file
Too complicated!
I just wanted a simple task to execute every file under db/seeds directory without passing in any file names.
# lib/tasks/seed.rake
desc "Run all files in db/seeds directory"
namespace :db do
task seed: :environment do
Dir[File.join(Rails.root, 'db', 'seeds', '*.rb')].each do |filename|
puts "seeding - #{filename}. for reals, yo!"
load(filename)
end
end
end
Related
I have folder db/seeds, which include about 20 files with default values for project.
Provide me correct setting for running command rake db:seed for load all this files.
Create one file at lib/tasks/. give name main_seed_file.rake to the new file. paste below code into main_seed_file.rake.
desc "Run all files in db/seeds directory"
namespace :db do
task seeds: :environment do
Dir[File.join(Rails.root, 'db', 'seeds', '*.rb')].each do |filename|
puts "seeding - #{filename}"
load(filename)
end
end
end
Now execute this rake db:seeds
Cheers!
I'm trying to user rake and rufus, both of which I am new to. I want to have Rufus call my rake task but I am getting the following error. Don't know how to build task 'inbox:process_inbox'
lib/tasks/inbox_tasks.rb
namespace :inbox do
task :process_inbox do
logger = Logger.new(Rails.root.to_s + "/log/scheduler.log")
logger.info "Rufus Here!"
end
end
rufus_scheduler.rb
require 'rufus-scheduler'
require 'rake'
scheduler = Rufus::Scheduler.new
scheduler.every '10s', :first_at => Time.now + 3 do
Rake::Task["inbox:process_inbox"]
end
As #jmettraux (the creator of rufus-scheduler!) has already answered, the problem is that the rake task is defined in a .rb file instead of .rake file.
Adding some more details to help in the future.
While creating a new rake task, you could get the rails generator to automatically create the file with appropriate structure.
Example: Running
> rails g task inbox process_inbox
create lib/tasks/inbox.rake
will create a file named lib/tasks/inbox.rake with content:
namespace :inbox do
desc "TODO"
task process_inbox: :environment do
end
end
Having a DESC in the task definition is important; that allows for verifying that the rake task is defined and available, by running either rake -T inbox or rake -T | grep inbox
> rake -T inbox
rake inbox:process_inbox # TODO
Could this one help?
How to build task 'db:populate' (renaming inbox_tasks.rb to inbox_tasks.rake)
(did a simple https://www.google.com/?#q=rails+don%27t+know+how+to+build+task ...)
What's the command if I wanted to run a task within test.rb under the following directory:
lib/tasks/lib/data/test.rb
To add a rake task you need to create a .rake file instead of .rb file in the lib/tasks directory as
lib/tasks/sample_task.rake
or in your case
lib/tasks/lib/data/test.rake
rake tasks are defined using namespace
Example test.rake file
# Namespace declaration
namespace :sample do
# Task Description
desc "Sample task"
# Here sample_task is the name of the task
task sample_task: :environment do
# Your task
5.times do |t|
puts "Hello world"
end
end
end
Now run your task with rake sample:sample_task
where sample is namespace declaration and sample_task is the name of the task
If you are using rails 2 then you can use runner to run your tasks like
script/runner sample_task
I'm using rails 4 and I need to add a subtask for seeding our database using demo data (for product demo's). I want to make it a subtask called rake db:seed:demo, how can I do this?
I tried to subtask using this code, but I got an error from rake saying task not found.
#!/usr/bin/env rake
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
require File.expand_path('../config/application', __FILE__)
API::Application.load_tasks
task :demo => :seed do
end
task :seed => :db
Use the namespace directive:
namespace :db do
namespace :seed do
task :demo do
end
end
end
I need to create a recurring task that creates (or edits) product records from an external text file. From inside irb:
>> f = File.open(<filename>) # file in same directory path
No issues.
But when pasted into a Rake task file, the script always bombs "File not found". (Rails 3.1, Ubuntu.)
namespace :sap do
desc "uploads data from raw SAP file"
task :upload => :environment do
f = File.open("sap_pnlist_20111010a.csv")
records = f.readlines
records.each {|row|
... etc etc ...
}
end
end
Suggestions?
If the file is somewhere inside your Rails root, use
Rails.root.join('grandparent_dir', 'parent_dir', 'file.txt')
If the file is not in your Rails root, you must give it the full path.