<?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/activerecord"/>
  <link href="http://persumi.com/u/fredwu/tech/e/blog/t/activerecord/feed/rss"/>
  <link rel="self" href="http://persumi.com/u/fredwu/tech/e/blog/t/activerecord/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/activerecord</id>
  <title>Blog (activerecord) - Fred Wu&apos;s Tech</title>
  <updated>2026-06-21T03:25:11.003064Z</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 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;</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;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;</content>
    <published>2011-07-06T12: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/comic-because-activerecord-is-slow"/>
    <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/comic-because-activerecord-is-slow</id>
    <title>comic because activerecord is slow</title>
    <updated>2011-07-06T12:12:00.000000Z</updated>
  </entry>
</feed>