Skip to content

Instantly share code, notes, and snippets.

@maxivak
Last active June 29, 2023 06:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maxivak/53ba2f6e2e750816f2dd to your computer and use it in GitHub Desktop.
Save maxivak/53ba2f6e2e750816f2dd to your computer and use it in GitHub Desktop.
Load 'seed' data in Rails - rake task. Details: http://maxivak.com/loading-seed-data-in-rails/

Rake task to load 'seed' data in Rails

1:
id: 1
name: "fruits"
title: "Fruits"
enabled: 1
2:
id: 2
name: "vegetables"
title: "Vegetables"
enabled: 1
FactoryGirl.create(modelname, attr)
# spec/factories/categories.rb
FactoryGirl.define do
factory :category do
end
end
# lib/db/dbloader.rb
require 'active_record'
require 'factory_girl_rails'
class Db::Dbloader
def self.load_all_YAML_from_files(pattern)
db_connect
Dir.glob(pattern).each do |path|
load_YAML_from path
end
end
def self.db_connect
unless ActiveRecord::Base.connected?
ActiveRecord::Base.establish_connection ENV['RAILS_ENV']
end
end
def self.load_YAML_from(path)
path =~ %r%([^/_]+)\.ya?ml$%
table = $1
modelname = table.singularize.to_sym
classname = modelname.to_s
classname[0] = classname[0].capitalize
cls = classname.constantize
#
data = YAML::load_file path
data.each do |id, attr|
if attr.has_key?('id') && (r=cls.find_by_id(attr['id']))
r.update attr
else
cls.create attr
#FactoryGirl.create(modelname, attr) # if !Product.find_by_name(product['name'])
end
end
end
end
1:
id: 1
name: "orange"
title: "Orange"
enabled: 1
category_id: 1
2:
id: 2
name: "apple"
title: "Apple"
enabled: 1
category_id: 1
3:
id: 3
name: "tomato"
title: "Tomato"
enabled: 1
category_id: 2
4:
id: 4
name: "potato"
title: "Potato"
enabled: 1
category_id: 2
# lib/tasks/db_seed_products.rake
namespace :db do
desc "Load YAML seed data from db/data/init"
namespace :seed do
namespace :products do
task :load => :environment do
require File.expand_path(File.dirname(__FILE__) + '../../../config/environment')
Dir[File.dirname(__FILE__) + '/../../app/models/*.rb'].each do |file|
require file
end
# load our library
require File.join( Rails.root, 'lib', 'db', 'dbloader.rb' )
# do the job
Db::Dbloader.load_all_YAML_from_files File.join( Rails.root, 'db', 'data', 'init', '*.yml' )
end
end
end
end
CREATE TABLE IF NOT EXISTS `categories` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`enabled` tinyint(1) NOT NULL DEFAULT '1',
`pos` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE IF NOT EXISTS `products` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`title` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`enabled` tinyint(1) NOT NULL DEFAULT '1',
`pos` int(11) NOT NULL DEFAULT '0',
`category_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment