Question

I've got a rails application where users have to log in. Therefore in order for the application to be usable, there must be one initial user in the system for the first person to log in with (they can then create subsequent users). Up to now I've used a migration to add a special user to the database.

After asking this question, it seems that I should be using db:schema:load, rather than running the migrations, to set up fresh databases on new development machines. Unfortunately, this doesn't seem to include the migrations which insert data, only those which set up tables, keys etc.

My question is, what's the best way to handle this situation:

  1. Is there a way to get d:s:l to include data-insertion migrations?
  2. Should I not be using migrations at all to insert data this way?
  3. Should I not be pre-populating the database with data at all? Should I update the application code so that it handles the case where there are no users gracefully, and lets an initial user account be created live from within the application?
  4. Any other options? :)
Was it helpful?

Solution 7

I thought I'd summarise some of the great answers I've had to this question, together with my own thoughts now I've read them all :)

There are two distinct issues here:

  1. Should I pre-populate the database with my special 'admin' user? Or should the application provide a way to set up when it's first used?
  2. How does one pre-populate the database with data? Note that this is a valid question regardless of the answer to part 1: there are other usage scenarios for pre-population than an admin user.

For (1), it seems that setting up the first user from within the application itself is quite a bit of extra work, for functionality which is, by definition, hardly ever used. It may be slightly more secure, however, as it forces the user to set a password of their choice. The best solution is in between these two extremes: have a script (or rake task, or whatever) to set up the initial user. The script can then be set up to auto-populate with a default password during development, and to require a password to be entered during production installation/deployment (if you want to discourage a default password for the administrator).

For (2), it appears that there are a number of good, valid solutions. A rake task seems a good way, and there are some plugins to make this even easier. Just look through some of the other answers to see the details of those :)

OTHER TIPS

Try a rake task. For example:

  1. Create the file /lib/tasks/bootstrap.rake
  2. In the file, add a task to create your default user:

    namespace :bootstrap do
      desc "Add the default user"
      task :default_user => :environment do
        User.create( :name => 'default', :password => 'password' )
      end

      desc "Create the default comment"
      task :default_comment => :environment do
        Comment.create( :title => 'Title', :body => 'First post!' )
      end

      desc "Run all bootstrapping tasks"
      task :all => [:default_user, :default_comment]
    end
  1. Then, when you're setting up your app for the first time, you can do rake db:migrate OR rake db:schema:load, and then do rake bootstrap:all.

Use db/seed.rb found in every Rails application.

While some answers given above from 2008 can work well, they are pretty outdated and they are not really Rails convention anymore.

Populating initial data into database should be done with db/seed.rb file.

It's just works like a Ruby file.

In order to create and save an object, you can do something like :

User.create(:username => "moot", :description => "king of /b/")

Once you have this file ready, you can do following

rake db:migrate

rake db:seed

Or in one step

rake db:setup

Your database should be populated with whichever objects you wanted to create in seed.rb

I recommend that you don't insert any new data in migrations. Instead, only modify existing data in migrations.

For inserting initial data, I recommend you use YML. In every Rails project I setup, I create a fixtures directory under the DB directory. Then I create YML files for the initial data just like YML files are used for the test data. Then I add a new task to load the data from the YML files.

lib/tasks/db.rake:

namespace :db do
  desc "This loads the development data."
  task :seed => :environment do
    require 'active_record/fixtures'
    Dir.glob(RAILS_ROOT + '/db/fixtures/*.yml').each do |file|
      base_name = File.basename(file, '.*')
      say "Loading #{base_name}..."
      Fixtures.create_fixtures('db/fixtures', base_name)
    end
  end

  desc "This drops the db, builds the db, and seeds the data."
  task :reseed => [:environment, 'db:reset', 'db:seed']
end

db/fixtures/users.yml:

test:
  customer_id: 1
  name: "Test Guy"
  email: "test@example.com"
  hashed_password: "656fc0b1c1d1681840816c68e1640f640c6ded12"
  salt: "188227600.754087929365988"

This is my new favorite solution, using the populator and faker gems:

http://railscasts.com/episodes/126-populating-a-database

Try the seed-fu plugin, which is quite a simple plugin that allows you to seed data (and change that seed data in the future), will also let you seed environment specific data and data for all environments.

I guess the best option is number 3, mainly because that way there will be no default user which is a great way to render otherwise good security useless.

Consider using the rails console. Good for one-off admin tasks where it's not worth the effort to set up a script or migration.

On your production machine:

script/console production

... then ...

User.create(:name => "Whoever", :password => "whichever")

If you're generating this initial user more than once, then you could also add a script in RAILS_ROOT/script/, and run it from the command line on your production machine, or via a capistrano task.

That Rake task can be provided by the db-populate plugin:

http://github.com/joshknowles/db-populate/tree/master

Great blog post on this: http://railspikes.com/2008/2/1/loading-seed-data

I was using Jay's suggestions of a special set of fixtures, but quickly found myself creating data that wouldn't be possible using the models directly (unversioned entries when I was using acts_as_versioned)

I'd keep it in a migration. While it's recommended to use the schema for initial setups, the reason for that is that it's faster, thus avoiding problems. A single extra migration for the data should be fine.

You could also add the data into the schema file, as it's the same format as migrations. You'd just lose the auto-generation feature.

For users and groups, the question of pre-existing users should be defined with respect to the needs of the application rather than the contingencies of programming. Perhaps your app requires an administrator; then prepopulate. Or perhaps not - then add code to gracefully ask for a user setup at application launch time.

On the more general question, it is clear that many Rails Apps can benefit from pre-populated date. For example, a US address holding application may as well contain all the States and their abbreviations. For these cases, migrations are your friend, I believe.

Some of the answers are outdated. Since Rails 2.3.4, there is a simple feature called Seed available in db/seed.rb :

#db/seed.rb
User.create( :name => 'default', :password => 'password' )
Comment.create( :title => 'Title', :body => 'First post!' )

It provides a new rake task that you can use after your migrations to load data :

rake db:seed

Seed.rb is a classic Ruby file, feel free to use any classic datastructure (array, hashes, etc.) and iterators to add your data :

["bryan", "bill", "tom"].each do |name|
  User.create(:name => name, :password => "password")
end

If you want to add data with UTF-8 characters (very common in French, Spanish, German, etc.), don't forget to add at the beginning of the file :

# ruby encoding: utf-8

This Railscast is a good introduction : http://railscasts.com/episodes/179-seed-data

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top