How (and whether) to populate rails application with initial data

后端 未结 13 1671
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 05:43

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 pe

相关标签:
13条回答
  • 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.

    0 讨论(0)
  • 2020-12-04 06:29

    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.

    0 讨论(0)
  • 2020-12-04 06:31

    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

    0 讨论(0)
  • 2020-12-04 06:32

    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.

    0 讨论(0)
  • 2020-12-04 06:32

    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 :)

    0 讨论(0)
  • 2020-12-04 06:35

    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"
    
    0 讨论(0)
提交回复
热议问题