views:

19

answers:

1

Rake db:seed populates your db with default database values for an app right? So what if you already have a seed and you need to add to it(you add a new feature that requires the seed). In my experience, when I ran rake db:seed again, it added the existing content already so existing content became double.

What I need is to add some seeds and when ran, it should just add the newest ones, and ignore the existing seeds. How do I go about with this? (the dirty, noob way I usually do it is to truncate my whole db then run seed again, but that's not very smart to do in production, right?)

+1  A: 

I do something like this.... When I need to add a user

in seeds.rb:

if User.count == 0
  puts "Creating admin user"
  User.create(:role=>:admin, :username=>'blagh', :etc=>:etc)
end

You can get more interesting than that, but in this case, you could run it over again as needed.

Jesse Wolgamott
hmmm so i could basically just add a count == 0 to any of the tables i need populated in my seed to ensure they won't duplicate right? thanks! I was also thinking of just doing a rake task for that
corroded
You may have to find specific records before creating them as well. Check for their presence: e.g. `User.create(:name => "Bob") unless User.find_by_name("Bob")`
Andy Atkinson