Joern Akkermann
Joern Akkermann

Reputation: 3622

How to add entries to a database table within a migration?

I just forgot how to do this and cannot find a helpful tutorial on the internet.

It is possible to setup a db table and then to fill it with data within the migration.

So I got my db "persons" with t.column :name => :string and want to add a person after the dbs creation. It was something like Person.add :name => "Nobody"... But I forgot how the method is called exactly.

Upvotes: 1

Views: 2138

Answers (3)

nathanvda
nathanvda

Reputation: 50057

You should never create new data in a migration. You can change existing data though.

To fill the database, you should use seeds.

This makes for a clean separation between defining the schema (migrations) and filling it with the correct data. One could assume you will need to change your seeds more often (e.g. a new look-up value is added). Seeding the database is an easy step, should be repeatable, and not effect the rest of the data.

Upvotes: 0

Sergey K
Sergey K

Reputation: 5107

Try to use krunal shah's sample in seed.rb. Then run rake task rake db:seed

Upvotes: 0

krunal shah
krunal shah

Reputation: 16339

Try this .. Person.create(:name => 'nobody')

Upvotes: 2

Related Questions