I found this interesting. If you use an after_initialize hook in an ActiveRecord model for some reason and you hit upon one of the attribute accessor methods, the following exception may reveal itself.
ActiveRecord::MissingAttributeError: missing attribute:
Specifically, I came across it when referencing the has_many side of an association from within a belongs_to model. For example:
class Person < ActiveRecord::Base belongs_to :person_group def after_initialize # Unsafe logger.info self.attribute.inspect # Safe to use logger.info self.attributes['attribute'].inspect end end class PersonGroup < ActiveRecord::Base has_many :people before_destroy :cb private def cb unless people_ids.blank? # Rails exception Person.update_all ... end end end
2 Comments
https://rails.lighthouseapp.com/projects/8994/tickets/3165-activerecordmissingattributeerror-after-update-to-rails-v-234
is the rails bug for this issue. You’re not alone ;)
Just got bit by this problem, thanks for writing it up so I could find it while searching!