How to run all files in folder seeds? - ruby-on-rails

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!

Related

include an module from rails app into rake task

I have a module in rails project on app/lib/md.rb and I want to import and use it in a rake task outside of application scope under lib/task directory. the app and lib folders are in same directory, in the other work it's like below:
- app
- lib
- md.rb
- lib
- task
my module is just couple of class and my rake task is like below:
include Md
task :import_product do
puts ''
puts '=================='
puts 'Started Fetching Products'
puts '=================='
...
end
This might be a possible duplicate, but nevertheless
task :import_product do
require File.join(Rails.root, 'app', 'lib', 'md.rb')
Md.hurray
puts ''
puts '=================='
puts 'Started Fetching Products'
puts '=================='
end
If you have more than one file to load from app/ folder its better to load with the environment in your rake task.
task import_product: :environment do
...
...
...
end
Hope it helps!

Running rake task on remote app, use file from local machine

How can I pass the csv file or file stream or something in line of that to the rake task I'm running on the remote app via rake task arguments?
So I can get the contents of that file in the file and do something with it. It's not a big file.
Update
I tried with suggestion from Luc:
desc 'Test task'
namespace :app do
task :pipe_file => [:environment] do |t, args|
puts "START"
File.open('my_temp_file', 'w') do |f2|
while line = STDIN.gets
f2.puts line
end
end
puts "DONE"
end
end
So when I run :
cat tst.csv | bundle exec rake app:pipe_file
Nothing happens, blank line prints
You can pipe the content of your file to your rake task:
cat my_file | heroku run rake --no-tty my_task
Then inside your task you need to start by reading STDIN:
STDIN.binmode
tmp_file = Tempfile.new('temp_file_prefix', Rails.root.join('tmp'))
tmp_file.write(STDIN.read)
tmp_file.close
Process tmp_file here.
puts tmp_file.path
tmp_file.unlink
Hope it helps !

Command for running a rails 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

Add sub-task rake

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

Adding a custom seed file

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

Resources