Topic: prepopulating DB [SOLVED]

by any chance is there a way to add a record to the DB using a migration like if you want to have 1 admin set up so when you reset the DB it would have this 1 user created?

Last edited by moiseszaragoza (2012-06-26 09:51:17)

Re: prepopulating DB [SOLVED]

You can just add a line like that in your migration:

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|
      t.string :name
      t.string :hashed_password
      t.string :salt
      t.timestamps
    end
    User.find_or_create_by_name(:name=>'admin', :password=>'admin')
  end
  
  def self.down
    drop_table :users
  end
end

Just adopt the code according to your User model. Another way to that is to use Rake tasks.
HTH

Re: prepopulating DB [SOLVED]

Thanks that helped me allot