Skip to content

Instantly share code, notes, and snippets.

@ppworks
Created January 5, 2012 04:46
Show Gist options
  • Select an option

  • Save ppworks/1563756 to your computer and use it in GitHub Desktop.

Select an option

Save ppworks/1563756 to your computer and use it in GitHub Desktop.
rails:db/seeds.rb
fugos:
-
id: 1
name: 'fugo'
-
id: 2
name: 'fugofugo'
hoges:
-
id: 1
name: 'hoge'
-
id: 2
name: 'hogehoge'
INSERT `piyos` VALUES(1, 'piyo');
[Hoge, Fugo].each do |c|
yml = File.read("#{Rails.root}/db/seeds/#{c.to_s.tableize}.yml")
list = YAML.load(yml).symbolize_keys
list[c.to_s.tableize.to_sym].each do |r|
c.create do |t|
r.each do |i, v|
t.send "#{i}=", v
end
end
end
end
if Rails.env.development?
connection = ActiveRecord::Base.connection()
[Piyo].each do |c|
if c.count == 0
open("#{Rails.root}/db/sql/#{c.to_s.tableize}_sample.sql") {|file|
while sql = file.gets
connection.execute sql
end
}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment