Hey all,
I created this in seeds.rb because I want to avoid duplication as much as possible:
class DataGenerator def initialize @current_user = User.first end
def posts! Title.each do |t| Body.each do |b| post! t, b, @current_user.id end end end end
class StaticDataGenerator < DataGenerator def initialize super @created_at = "2011-04-04".to_date @updated_at = "2011-05-04".to_date @published = false end
def post!(title, body, user_id, options = {}) defaults = { :title => title, :body => body, :created => @created_at, :updated => @updated_at, :published => @published, :user_id => user_id } blog_post = BlogPost.create!(defaults.merge(options)) end end
class RandomDataGenerator < DataGenerator def initialize super end end
Title = ["ABC","DEF","GHI","JKL"] Body = ["MNO","PQR","STU","VWX"]
So basically what should happen is a new blog_posts record is written to the database populated with default values like in the following sql statement: INSERT INTO blog_posts (title,body,created_at,updated_at,published,user_id) VALUES("ABC","MNO",2011-04-04,2011-05-04,0,1)
However, when I run rake db:seed, nothing happens. I suspect that it's the class structure of my script that's not being triggered when I run db:seed. Any idea how to address this?
Thanks for response.