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 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.
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
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 3 seed file loads everything but data for User model
Try using .create!
instead to make it throw and exception if something is going wrong with validation.
simulate timestamp in seeds.rb
The problem is this line:
product.created_at = i.days.ago,
You need to get rid of the trailing comma, that's why you're ending up with an array for created_at
. Fix that and you can get rid of the before_save
callback.
Edit: The reason you are getting the ---
in there is because whatever ORM you are using is trying to serialize the Array and it's turning it into YAML.
simulate timestamp in seeds.rb
The problem is this line:
product.created_at = i.days.ago,
You need to get rid of the trailing comma, that's why you're ending up with an array for created_at
. Fix that and you can get rid of the before_save
callback.
Edit: The reason you are getting the ---
in there is because whatever ORM you are using is trying to serialize the Array and it's turning it into YAML.
Related Topics
Creating a Test Suite Using Ruby Minitest
Uploading a File to a Website with Ruby/Rails
How to Fix a Slow Implicit Query on Pg_Attribute Table in Rails
In Ruby or Rails, Why Is "Include" Sometimes Inside the Class and Sometimes Outside the Class
Check If Array of Integers Increments in Ruby
Rails How to Change Attribute Name When Rendering JSON
Ruby Map Method Syntax Question
Ruby: Uri::Invalidurierror (Uri Must Be Ascii Only
Finding the Ip Address of a Domain
Why Does Hash.New({}) Hide Hash Members
Converting an Array of Hashes to One Hash in Ruby
Phonegap Mobile Rails Authentication (Devise? Authentication from Scratch)
Expected #Count to Have Changed by 1, But Was Not Given a Block
Get Response Headers from Curb
Ruby on Rails Map.Root Doesn't Seem to Be Working
How to Render a String as an Erb File