<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <link>http://persumi.com/u/fredwu/tech/e/blog/t/migration</link>
    <generator>Persumi - Level up your writing and blogging with AI</generator>
    <category>Blog</category>
    <category>Tech</category>
    <pubDate>Sun, 21 Jun 2026 03:27:16 +0000</pubDate>
    <description/>
    <title>Blog (migration) - Fred Wu&apos;s Tech</title>
    <atom:link type="application/rss+xml" rel="self" href="http://persumi.com/u/fredwu/tech/e/blog/t/migration/feed/rss"></atom:link>
    <item>
      <pubDate>Wed, 27 Feb 2013 00:12:00 +0000</pubDate>
      <guid>http://persumi.com/u/fredwu/tech/e/blog/p/activerecord-and-db-migration-ate-my-model-attributes</guid>
      <comments>http://persumi.com/u/fredwu/tech/e/blog/p/activerecord-and-db-migration-ate-my-model-attributes</comments>
      <category>Blog</category>
      <category>Tech</category>
      <author>ifredwu@gmail.com (Fred Wu)</author>
      <description>&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 class=&quot;inline&quot;&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 #&lt;Ironman:0x00000008525d20&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;code class=&quot;inline&quot;&gt;attack_power&lt;/code&gt; is a new attribute we recently added to the &lt;code class=&quot;inline&quot;&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;ruby language-ruby&quot;&gt;Ironman
# =&gt; Ironman(id: integer, created_at: datetime, updated_at: datetime)

Ironman.column_names
# =&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;ruby language-ruby&quot;&gt;ActiveRecord::Base.connection.structure_dump
# =&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 class=&quot;inline&quot;&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 class=&quot;inline&quot;&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;ruby 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 class=&quot;inline&quot;&gt;Rakefile&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;ruby 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 class=&quot;inline&quot;&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;ruby language-ruby&quot;&gt;system &apos;rake db:reset&apos;&lt;/code&gt;&lt;/pre&gt;
]]&gt;</description>
      <link>http://persumi.com/u/fredwu/tech/e/blog/p/activerecord-and-db-migration-ate-my-model-attributes</link>
      <title>ActiveRecord and DB Migration Ate My Model Attributes!</title>
    </item>
    <item>
      <pubDate>Thu, 22 Jul 2010 12:30:50 +0000</pubDate>
      <guid>http://persumi.com/u/fredwu/tech/e/blog/p/rails-introducing-datamappify-activerecord-without-db-migrations</guid>
      <comments>http://persumi.com/u/fredwu/tech/e/blog/p/rails-introducing-datamappify-activerecord-without-db-migrations</comments>
      <category>Blog</category>
      <category>Tech</category>
      <author>ifredwu@gmail.com (Fred Wu)</author>
      <description>&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;</description>
      <link>http://persumi.com/u/fredwu/tech/e/blog/p/rails-introducing-datamappify-activerecord-without-db-migrations</link>
      <title>[Rails] Introducing Datamappify - ActiveRecord Without DB Migrations</title>
    </item>
    <item>
      <pubDate>Tue, 25 May 2010 10:58:00 +0000</pubDate>
      <guid>http://persumi.com/u/fredwu/tech/e/blog/p/rails-tip-model-attributes-not-updating-reset-column-information-to-the-rescue</guid>
      <comments>http://persumi.com/u/fredwu/tech/e/blog/p/rails-tip-model-attributes-not-updating-reset-column-information-to-the-rescue</comments>
      <category>Blog</category>
      <category>Tech</category>
      <author>ifredwu@gmail.com (Fred Wu)</author>
      <description>&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;ruby language-ruby&quot;&gt;class CreateJobLevels &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 =&gt; type)
    end
  end

  def self.down
    drop_table :job_levels
  end
end&lt;/code&gt;&lt;/pre&gt;
]]&gt;</description>
      <link>http://persumi.com/u/fredwu/tech/e/blog/p/rails-tip-model-attributes-not-updating-reset-column-information-to-the-rescue</link>
      <title>[Rails Tip] Model Attributes Not Updating? `reset_column_information` To the Rescue!</title>
    </item>
  </channel>
</rss>