I have a new rails app with a fresh database and I want to add some default entries, like admin with default password etc.
How should I proceed?
I know of two possibilities, that both have drawbacks:
- Use ruby code in migration: User.create!(:email => “admin@example.com”, :password => “abc”)
- Use SQL code in migration: INSERT INTO users (id, email, password) VALUES (1, ‘admin@example.com’, ‘abc’)
The first alternative can break if I alter my code in later versions. The second is somewhat DBMS dependent.
As I do not plan to change my database, I would go with SQL code, but are there better alternatives?
Personally I’d use seed data
http://railscasts.com/episodes/179-seed-data
One advantage of this is that rake db:setup calls rake db:seed after it has created the database. Which is perfect for new machines.