<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <generator>Persumi - Level up your writing and blogging with AI</generator>
  <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
  <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
  <link href="http://persumi.com/u/fredwu/tech/e/blog/t/migration"/>
  <link href="http://persumi.com/u/fredwu/tech/e/blog/t/migration/feed/rss"/>
  <link rel="self" href="http://persumi.com/u/fredwu/tech/e/blog/t/migration/feed/atom"/>
  <author>
    <name>Fred Wu</name>
    <email>ifredwu@gmail.com</email>
    <uri>http://persumi.com/u/fredwu</uri>
  </author>
  <subtitle/>
  <id>http://persumi.com/u/fredwu/tech/e/blog/t/migration</id>
  <title>Blog (migration) - Fred Wu&apos;s Tech</title>
  <updated>2026-11-03T23:18:39.737061Z</updated>
  <entry>
    <content type="html">&lt;![CDATA[&lt;p&gt;&lt;em&gt;Update: You might also want to check out &lt;a href=&quot;/blog/2010-05-25-rails-tip-model-attributes-not-updating/&quot;&gt;&lt;code&gt;reset_column_information&lt;/code&gt;&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;So a few days ago we started seeing the following errors on our Jenkins builds (swapped with fictional model and attribute names):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;NoMethodError:
  undefined method `attack_power=&apos; for #&amp;lt;Ironman:0x00000008525d20&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;attack_power&lt;/code&gt; is a new attribute we recently added to the &lt;code&gt;Ironman&lt;/code&gt; ActiveRecord model.&lt;/p&gt;
&lt;p&gt;I was baffled, as the table column is clearly there but ActiveRecord couldn’t see it.&lt;/p&gt;
&lt;p&gt;This weird behaviour is confirmed by debugging the model:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby&quot;&gt;Ironman
# =&amp;gt; Ironman(id: integer, created_at: datetime, updated_at: datetime)

Ironman.column_names
# =&amp;gt; [&quot;id&quot;, &quot;created_at&quot;, &quot;updated_at&quot;]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And by debugging the schema:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby&quot;&gt;ActiveRecord::Base.connection.structure_dump
# =&amp;gt; &quot;CREATE TABLE `ironmans` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `created_at` datetime NOT NULL,\n `updated_at` datetime NOT NULL,\n `attack_power` int(11) NOT NULL,\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;\n\n&quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So apparently somehow ActiveRecord ate our &lt;code&gt;attack_power&lt;/code&gt; attribute!&lt;/p&gt;
&lt;p&gt;After some struggling and head-scratching, finally I’ve discovered that during the migration if we call the &lt;code&gt;Ironman&lt;/code&gt; class, then any subsequent attribute changes to the class will not be recognised by ActiveRecord.&lt;/p&gt;
&lt;p&gt;It turns out, because we run our test suites by invoking rake tasks:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby&quot;&gt;Rake::Task[&apos;spec:something&apos;].invoke
Rake::Task[&apos;spec:something_else&apos;].invoke
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And we manually reset our database beforehand too within the same &lt;code&gt;Rakefile&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby&quot;&gt;Rake::Task[&apos;db:reset&apos;].invoke
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;ActiveRecord will cache its attributes as soon as a model class (&lt;code&gt;Ironman&lt;/code&gt;) is called during the migration.&lt;/p&gt;
&lt;p&gt;The fix? Simple! Simply use another process to run the database reset:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby&quot;&gt;system &apos;rake db:reset&apos;
&lt;/code&gt;&lt;/pre&gt;]]&gt;</content>
    <published>2013-02-27T00:12:00.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/activerecord-and-db-migration-ate-my-model-attributes"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/activerecord-and-db-migration-ate-my-model-attributes</id>
    <title>ActiveRecord and DB Migration Ate My Model Attributes!</title>
    <updated>2013-02-27T00:12:00.000000Z</updated>
  </entry>
  <entry>
    <content type="html">&lt;![CDATA[&lt;h2&gt;Introduction&lt;/h2&gt;
&lt;p&gt;ActiveRecord is without doubt the &lt;em&gt;de facto&lt;/em&gt; ORM library for Rails and many Ruby web frameworks. Many developers however, do not like database migrations and prefer to use DSL for data mapping. Datamappify is created with the sole purpose of getting rid of the DB migration headaches.&lt;/p&gt;
&lt;h2&gt;Why Not DB Migrations?&lt;/h2&gt;
&lt;p&gt;Well, depending on your specific project, DB migrations might create more trouble than it’s worth. Besides, your code is already version controlled, so why create a separate version control for your DB schema?&lt;/p&gt;
&lt;h2&gt;Why Not Use DataMapper, Sequel, etc?&lt;/h2&gt;
&lt;p&gt;As stated in the introduction, ActiveRecord is the most popular ORM in the rails community, it is actively developed and battle-tested. If your only grief with ActiveRecord is the DB migrations, why not just eliminate it be happy? ;)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href=&quot;http://github.com/fredwu/datamappify&quot;&gt;Go check out the code&lt;/a&gt;!&lt;/strong&gt;&lt;/p&gt;]]&gt;</content>
    <published>2010-07-22T12:30:50.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/rails-introducing-datamappify-activerecord-without-db-migrations"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/rails-introducing-datamappify-activerecord-without-db-migrations</id>
    <title>[Rails] Introducing Datamappify - ActiveRecord Without DB Migrations</title>
    <updated>2010-07-22T12:30:50.000000Z</updated>
  </entry>
  <entry>
    <content type="html">&lt;![CDATA[&lt;p&gt;So you were wondering why some of your model attributes weren’t updating properly? Well, it is perhaps because the db schema has changed but the changed schema has not been passed onto ActiveRecord, as is often the case in DB migration.&lt;/p&gt;
&lt;p&gt;Taken from the ActiveRecord documentation:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Resets all the cached information about columns, which will cause them to be reloaded on the next request.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;The most common usage pattern for this method is probably in a migration, when just after creating a table you want to populate it with some default values, eg:&lt;/em&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ruby&quot;&gt;class CreateJobLevels &amp;lt; ActiveRecord::Migration
  def self.up
    create_table :job_levels do |t|
      t.integer :id
      t.string :name

      t.timestamps
    end

    JobLevel.reset_column_information
    %w{assistant executive manager director}.each do |type|
      JobLevel.create(:name =&amp;gt; type)
    end
  end

  def self.down
    drop_table :job_levels
  end
end
&lt;/code&gt;&lt;/pre&gt;]]&gt;</content>
    <published>2010-05-25T10:58:00.000000Z</published>
    <category label="Blog" scheme="http://persumi.com/u/fredwu/tech/e/blog" term="blog"/>
    <category label="Tech" scheme="http://persumi.com/u/fredwu/tech" term="tech"/>
    <link href="http://persumi.com/u/fredwu/tech/e/blog/p/rails-tip-model-attributes-not-updating-reset-column-information-to-the-rescue"/>
    <author>
      <name>Fred Wu</name>
      <email>ifredwu@gmail.com</email>
      <uri>http://persumi.com/u/fredwu</uri>
    </author>
    <id>http://persumi.com/u/fredwu/tech/e/blog/p/rails-tip-model-attributes-not-updating-reset-column-information-to-the-rescue</id>
    <title>[Rails Tip] Model Attributes Not Updating? `reset_column_information` To the Rescue!</title>
    <updated>2010-05-25T10:58:00.000000Z</updated>
  </entry>
</feed>