Skip to content

Instantly share code, notes, and snippets.

@pwim
Created June 23, 2011 06:56
Show Gist options
  • Save pwim/1042058 to your computer and use it in GitHub Desktop.
Save pwim/1042058 to your computer and use it in GitHub Desktop.
Rails 3.1 and after_intialize
class Invoice < ActiveRecord::Base
belongs_to :sender
after_initialize :assign_defaults
def assign_defaults
if new_record? && sender
self.fax ||= sender.fax
end
end
end
invoice = Sender.first.invoices.new # sender is nil in assign_defaults
invoice.sender # returns sender properly
@pwim
Copy link
Author

pwim commented Jun 23, 2011

Same issue if I use default_value_for plugin (https://github.com/boblail/default_value_for works under 3.1). Has the ordering of after_intialize been changed in 3.1?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment