While upgrading Ruby on Rails from 2.1 to 2.3, one of my models threw an ActiveRecord::MissingAttributeError exception when something used exists? to see if it was there. The issue turned out to be our after_initialize hook on the model. In our hook, we rely on looking up the value of one of the object attributes. This fails now because exists? only selects the primary key from the table, leaving the rest of the attributes undefined. Here is how we solved it:

  1. def after_initialize
  2.   do_something if foo_attribute
  3. rescue ActiveRecord::MissingAttributeError
  4.   reload
  5.   retry
  6. end

This will cause all the attributes to be fleshed out the second time around.