create with a block and after_initialize have inconsistant behavior#2074

Labels

Milestone

Assignee

3 participants

The problem I am running into when I create and object that uses after_initialize to set up default values for attributes that are dependent on other attributes and set some of the values for the new object in a block.

Let's say I have the following class with an after_initialize to set up the msrp based on the given wholesale value.

I know why this happens. It happens because create (in ActiveRecord::Base) with a block given calls new first, which triggers after_initialize, then yields to the block with the new object. Since the wholesale value is set in the block, after_initialize doesn't have access to it to set msrp.

Sorry for not providing a patch, I haven't dug around enough to figure out what would be the best way to adress this. Figured it was important enough to at least raise the issue first.