Split seeds.rb into multiple sections?
In ./db/seeds/my_module.rb
:
module MyModule
puts "In my_module.rb"
# add code here
end
In ./db/seeds.rb
:
require File.expand_path('../seeds/my_module', __FILE__) # the ../ just removes `seeds.rb` filename from the path which is given by __FILE__
p "In seeds.rb"
# add code here
Split seeds.rb file
Since your seeds.rb
file is Ruby, then you can do anything in it you can do in Ruby. Like routes.rb
and Gemfile
you can get quite creative if required.
Just be sure that you don't do anything so crazy it would actually require some debugging. As long as you keep your actions clear, there should be no confusion. That is, don't use custom helper methods that are defined in some other file that could malfunction and would have to be tracked down by hand. It's probably best to stick to the simplest solution when doing things like this.
How can I split seeds.rb to populated different data in development vs. production?
You just need use the Rails.env
to know which environnement you are and switch data.
Is there any way to have multiple seeds.rb files? Any kind of 'versioning' for seed data?
You can re-use the seed
task, but make it idempotent.
To make the seed idempotent, simply check for the existence of the condition before executing a command. An example: do you want to create a new admin user?
User.find_or_create_by_username(:username => "admin")
instead of
User.create(:username => "admin")
However, seed
should be used to populate your database when the project is created. If you want to perform complex data seeding durin the lifecycle of the app, simply create a new rake task, execute it then remove it.
How to create seeds.rb array?
The reason it didn't work is that you actually passed three arrays with two hashes in each of them.
Pass a single array to the #create
method with a single hash for each record you want to create.
For example:
Department.create([{ deptitle: 'dept 1', deptdescription: 'this is the first dept' },
{ depttitle: 'dept 2', deptdescription: 'this is the second dept' }])
But instead of 'creating an array' you could use a simple loop to create Department records.
10.times do |x|
Department.create({deptitle: "dept #{x}", deptdescription: "this is the #{x} department"})
end
In my opinion it looks cleaner, takes less place and it is easier to change number of seed records if you need to.
To create a numeral from a number (for "this is the Xst dept" sentence) you could use a humanize gem.
Is there a way to db:seed only one table in Rails?
With below custom rake file, you can have multiple seed files in db/seeds/
folder.
# lib/tasks/custom_seed.rake
# lib/tasks/custom_seed.rake
namespace :db do
namespace :seed do
Dir[File.join(Rails.root, 'db', 'seeds', '*.rb')].each do |filename|
task_name = File.basename(filename, '.rb').intern
task task_name => :environment do
load(filename)
end
end
task :all => :environment do
Dir[File.join(Rails.root, 'db', 'seeds', '*.rb')].sort.each do |filename|
load(filename)
end
end
end
end
Then, in order to run specific seed file, you can just run
rake db:seed:seed_file_name
To run all the seeds file with order in that db/seeds
folder, run below command
rake db:seed:all
Rails Seed Data from file
You need to pass an id to delete.
I assume you want to delete_all
International.delete_all
Rails How to use FactoryGirl in Seeds.rb?
Seeds.rb
require Rails.root.join('spec','helpers.rb')
require 'rubygems' #so it can load gems
require 'factory_girl_rails' #so it can run in development
seed_data
spec/helpers.rb
def seed_data
Factory.create(:user)
end
Related Topics
MAC Os X Mountain Lion "Rails Is Not Currently Installed on This System."
How to Use Rails and Paperclip to Store Photos on Google Cloud Storage
Configuring Jekyll for Github Project Pages
Define a Method That Is a Closure in Ruby
Switching Between Web and Touch Interfaces on Facebook Login Using Omniauth and Rails 3
How to Switch to Older Versions of the Ruby/Rails Environment
How to Controller (Start/Kill) a Background Process (Server App) in Ruby
How to Monitor Uptime of 20 Websites (Ping or Http) in Node.Js/Ror
How to Call Applicationcontroller Methods from Applicationhelper
Creating an Md5 Hash of a Number, String, Array, or Hash in Ruby
No Database Connection in Rails Console
Rails Scope Find with Current User
What Are the Uppercase and Lowercase Rules of Ruby Method Name
Resque Multiple Workers in Development Mode
Generating Unique Token on the Fly with Rails