<p>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.</p>

<p>After asking <a href="https://stackoverflow.com/questions/62038/rails-model-validators-break-earlier-migrations">this question</a>, 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.</p>

<p>My question is, what's the best way to handle this situation:</p>

<ol>
<li>Is there a way to get d:s:l to include data-insertion migrations?</li>
<li>Should I not be using migrations at all to insert data this way?</li>
<li>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?</li>
<li>Any other options? :)</li>
</ol>

[{'answer_id': 62206, 'author': 'Matthias Winkelmann', 'author_id': 4494, 'author_profile': 'https://Stackoverflow.com/users/4494', 'pm_score': 2, 'selected': False, 'text': "<p>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.</p>\n\n<p>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.</p>\n"}, {'answer_id': 62214, 'author': 'Vinko Vrsalovic', 'author_id': 5190, 'author_profile': 'https://Stackoverflow.com/users/5190', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 62262, 'author': 'Trevor Stow', 'author_id': 75093, 'author_profile': 'https://Stackoverflow.com/users/75093', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n\n<p>On your production machine:</p>\n\n<pre><code>script/console production\n</code></pre>\n\n<p>... then ...</p>\n\n<pre><code>User.create(:name =&gt; "Whoever", :password =&gt; "whichever")\n</code></pre>\n\n<p>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.</p>\n'}, {'answer_id': 62528, 'author': 'Aaron Wheeler', 'author_id': 6940, 'author_profile': 'https://Stackoverflow.com/users/6940', 'pm_score': 6, 'selected': False, 'text': '<p>Try a rake task.  For example:</p>\n\n<ol>\n<li>Create the file <strong>/lib/tasks/bootstrap.rake</strong></li>\n<li>In the file, add a task to create your default user:</li>\n</ol>\n\n<pre><code>\n    namespace :bootstrap do\n      desc "Add the default user"\n      task :default_user => :environment do\n        User.create( :name => \'default\', :password => \'password\' )\n      end\n\n      desc "Create the default comment"\n      task :default_comment => :environment do\n        Comment.create( :title => \'Title\', :body => \'First post!\' )\n      end\n\n      desc "Run all bootstrapping tasks"\n      task :all => [:default_user, :default_comment]\n    end\n</code></pre>\n\n<ol start="3">\n<li>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.</li>\n</ol>\n'}, {'answer_id': 64712, 'author': 'NickR', 'author_id': 6941, 'author_profile': 'https://Stackoverflow.com/users/6941', 'pm_score': 2, 'selected': False, 'text': '<p>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. </p>\n\n<p>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. </p>\n'}, {'answer_id': 64822, 'author': 'Jay Stramel', 'author_id': 3547, 'author_profile': 'https://Stackoverflow.com/users/3547', 'pm_score': 5, 'selected': False, 'text': '<p>I recommend that you don\'t insert any <em>new</em> data in migrations.  Instead, only modify existing data in migrations.</p>\n\n<p>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.</p>\n\n<p><strong>lib/tasks/db.rake:</strong></p>\n\n<pre><code>namespace :db do\n  desc "This loads the development data."\n  task :seed =&gt; :environment do\n    require \'active_record/fixtures\'\n    Dir.glob(RAILS_ROOT + \'/db/fixtures/*.yml\').each do |file|\n      base_name = File.basename(file, \'.*\')\n      say "Loading #{base_name}..."\n      Fixtures.create_fixtures(\'db/fixtures\', base_name)\n    end\n  end\n\n  desc "This drops the db, builds the db, and seeds the data."\n  task :reseed =&gt; [:environment, \'db:reset\', \'db:seed\']\nend\n</code></pre>\n\n<p><strong>db/fixtures/users.yml:</strong></p>\n\n<pre><code>test:\n  customer_id: 1\n  name: "Test Guy"\n  email: "test@example.com"\n  hashed_password: "656fc0b1c1d1681840816c68e1640f640c6ded12"\n  salt: "188227600.754087929365988"\n</code></pre>\n'}, {'answer_id': 66954, 'author': 'pantulis', 'author_id': 3972, 'author_profile': 'https://Stackoverflow.com/users/3972', 'pm_score': 2, 'selected': False, 'text': '<p>That Rake task can be provided by the db-populate plugin:</p>\n\n<p><a href="http://github.com/joshknowles/db-populate/tree/master" rel="nofollow noreferrer">http://github.com/joshknowles/db-populate/tree/master</a></p>\n'}, {'answer_id': 70650, 'author': 'DEfusion', 'author_id': 6432, 'author_profile': 'https://Stackoverflow.com/users/6432', 'pm_score': 3, 'selected': False, 'text': '<p>Try the <a href="http://www.intridea.com/2008/4/20/seed-fu-simple-seed-data-for-rails" rel="noreferrer">seed-fu</a> 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.</p>\n'}, {'answer_id': 71764, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>This is my new favorite solution, using the populator and faker gems:</p>\n\n<p><a href="http://railscasts.com/episodes/126-populating-a-database" rel="noreferrer">http://railscasts.com/episodes/126-populating-a-database</a></p>\n'}, {'answer_id': 76136, 'author': 'Luke Halliwell', 'author_id': 3974, 'author_profile': 'https://Stackoverflow.com/users/3974', 'pm_score': 3, 'selected': True, 'text': "<p>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 :)</p>\n\n<p>There are two distinct issues here:</p>\n\n<ol>\n<li>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?</li>\n<li>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.</li>\n</ol>\n\n<p>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).</p>\n\n<p>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 :)</p>\n"}, {'answer_id': 3682143, 'author': 'Kevin Davis', 'author_id': 49993, 'author_profile': 'https://Stackoverflow.com/users/49993', 'pm_score': 2, 'selected': False, 'text': '<p>Great blog post on this:\n<a href="http://railspikes.com/2008/2/1/loading-seed-data" rel="nofollow noreferrer">http://railspikes.com/2008/2/1/loading-seed-data</a></p>\n\n<p>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)</p>\n'}, {'answer_id': 14864024, 'author': 'Jason Kim', 'author_id': 536890, 'author_profile': 'https://Stackoverflow.com/users/536890', 'pm_score': 5, 'selected': False, 'text': '<p>Use <strong><code>db/seed.rb</code></strong> found in every Rails application.</p>\n\n<p>While some answers given above from <strong>2008</strong> can work well, they are pretty outdated and they are not really Rails convention anymore.</p>\n\n<p>Populating initial data into database should be done with <code>db/seed.rb</code> file.</p>\n\n<p>It\'s just works like a Ruby file.</p>\n\n<p>In order to create and save an object, you can do something like :</p>\n\n<p><code>User.create(:username =&gt; "moot", :description =&gt; "king of /b/")</code></p>\n\n<p>Once you have this file ready, you can do following</p>\n\n<p><code>rake db:migrate</code></p>\n\n<p><code>rake db:seed</code></p>\n\n<p>Or in one step</p>\n\n<p><code>rake db:setup</code></p>\n\n<p>Your database should be populated with whichever objects you wanted to create in seed.rb</p>\n'}, {'answer_id': 21256371, 'author': 'frenci', 'author_id': 2273475, 'author_profile': 'https://Stackoverflow.com/users/2273475', 'pm_score': 1, 'selected': False, 'text': '<p>Some of the answers are outdated. Since Rails 2.3.4, there is a simple feature called Seed available in <code>db/seed.rb</code> :</p>\n\n<pre><code>#db/seed.rb\nUser.create( :name =&gt; \'default\', :password =&gt; \'password\' )\nComment.create( :title =&gt; \'Title\', :body =&gt; \'First post!\' )\n</code></pre>\n\n<p>It provides a new rake task that you can use after your migrations to load data :</p>\n\n<pre><code>rake db:seed\n</code></pre>\n\n<p>Seed.rb is a classic Ruby file, feel free to use any classic datastructure (array, hashes, etc.) and iterators to add your data :</p>\n\n<pre><code>["bryan", "bill", "tom"].each do |name|\n  User.create(:name =&gt; name, :password =&gt; "password")\nend\n</code></pre>\n\n<p>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 :</p>\n\n<pre><code># ruby encoding: utf-8\n</code></pre>\n\n<p>This Railscast is a good introduction : <a href="http://railscasts.com/episodes/179-seed-data" rel="nofollow">http://railscasts.com/episodes/179-seed-data</a></p>\n'}]
['https://Stackoverflow.com/questions/62201', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3974/']