<p>I have a sequence of migrations in a rails app which includes the following steps:</p>

<ol>
<li>Create basic version of the 'user' model</li>
<li>Create an instance of this model - there needs to be at least one initial user in my system so that you can log in and start using it</li>
<li>Update the 'user' model to add a new field / column.</li>
</ol>

<p>Now I'm using "validates_inclusion_of" on this new field/column.  This worked fine on my initial development machine, which already had a database with these migrations applied.  However, if I go to a fresh machine and run all the migrations, step 2 fails, because validates_inclusion_of fails, because the field from migration 3 hasn't been added to the model class yet.</p>

<p>As a workaround, I can comment out the "validates_..." line, run the migrations, and uncomment it, but that's not nice.</p>

<p>Better would be to re-order my migrations so the user creation (step 2) comes last, after all columns have been added.</p>

<p>I'm a rails newbie though, so I thought I'd ask what the preferred way to handle this situation is :)</p>

[{'answer_id': 62148, 'author': 'Ben Scofield', 'author_id': 6478, 'author_profile': 'https://Stackoverflow.com/users/6478', 'pm_score': 4, 'selected': True, 'text': "<p>The easiest way to avoid this issue is to use <code>rake db:schema:load</code> on the second machine, instead of db:migrate. <code>rake db:schema:load</code> uses schema.rb to load the most current version of your schema, as opposed to migrating it up form scratch.</p>\n<p>If you run into this issue when deploying to a production machine (where preserving data is important), you'll probably have to consolidate your migrations into a single file without conflicts.</p>\n"}, {'answer_id': 63073, 'author': 'Étienne Barrié', 'author_id': 7489, 'author_profile': 'https://Stackoverflow.com/users/7489', 'pm_score': 3, 'selected': False, 'text': "<p>You can declare a class with the same name inside the migration, it will override your app/models one:</p>\n\n<pre><code>class YourMigration &lt; ActiveRecord::Migration\n\n  class User &lt; ActiveRecord::Base; end\n\n  def self.up\n    # User.create(:name =&gt; 'admin')\n  end\n\nend\n</code></pre>\n\n<p>Unfortunately, your IDE may try to autocomplete based on this class (Netbeans does) and you can't use your model logic in there (except if you duplicate it). </p>\n"}, {'answer_id': 1463960, 'author': 'dznqbit', 'author_id': 44940, 'author_profile': 'https://Stackoverflow.com/users/44940', 'pm_score': 0, 'selected': False, 'text': '<p>I\'m having to do this right now. Building upon BiHi\'s advice, I\'m loading the model manually then redefining methods where I need to.</p>\n\n<pre><code>load(File.join(RAILS_ROOT,"app/models/user.rb"))\nclass User &lt; ActiveRecord::Base\n  def before_validation; nil; end # clear out the breaking before_validation\n  def column1; "hello"; end       # satisfy validates_inclusion_of :column1\nend\n</code></pre>\n'}, {'answer_id': 62923603, 'author': 'Rael Gugelmin Cunha', 'author_id': 1604312, 'author_profile': 'https://Stackoverflow.com/users/1604312', 'pm_score': 0, 'selected': False, 'text': "<p>In your migration, you can save your user skipping ActiveRecord validation:</p>\n<pre><code>class YourMigration &lt; ActiveRecord::Migration\n\n  def up\n    user = User.new(name: 'admin')\n    user.save(validate: false)\n  end\n\nend\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/62038', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3974/']