<?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/activerecord</link>
    <generator>Persumi - Level up your writing and blogging with AI</generator>
    <category>Blog</category>
    <category>Tech</category>
    <pubDate>Sun, 21 Jun 2026 03:24:56 +0000</pubDate>
    <description/>
    <title>Blog (activerecord) - Fred Wu&apos;s Tech</title>
    <atom:link type="application/rss+xml" rel="self" href="http://persumi.com/u/fredwu/tech/e/blog/t/activerecord/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>Wed, 06 Jul 2011 12:12:00 +0000</pubDate>
      <guid>http://persumi.com/u/fredwu/tech/e/blog/p/comic-because-activerecord-is-slow</guid>
      <comments>http://persumi.com/u/fredwu/tech/e/blog/p/comic-because-activerecord-is-slow</comments>
      <category>Blog</category>
      <category>Tech</category>
      <author>ifredwu@gmail.com (Fred Wu)</author>
      <description>&lt;![CDATA[&lt;p&gt;
  &lt;img src=&quot;https://cdn.persumi.com/uploads/images/posts/1ee22517-8bfc-676a-b1f2-ce61dc92750f/imported/img/posts/old/tumblr_lnwv8teVP01qb7ot5o1_r3_1280.png&quot; alt=&quot;&quot; /&gt;
&lt;/p&gt;
&lt;p&gt;
Comic: Because ActiveRecord is Slow!&lt;/p&gt;
]]&gt;</description>
      <link>http://persumi.com/u/fredwu/tech/e/blog/p/comic-because-activerecord-is-slow</link>
      <title>comic because activerecord is slow</title>
    </item>
  </channel>
</rss>