Skip to content

Loading…

Fix after_initialize and Base.create edge case #2194

Merged
merged 2 commits into from

3 participants

@cldwalker

Reviewed #2074, simplified its tests and rebased off 3-1-stable

hmcfletch and others added some commits
@hmcfletch

Is there a reason this fix shouldn't go out in a 3.0.x release? If there is going to be another one...

@spastorino spastorino merged commit 1983007 into rails:3-1-stable
@spastorino
Ruby on Rails member

@cldwalker can you provide this same fix for master and 3-0-stable, please?

@spastorino spastorino added a commit that referenced this pull request
@spastorino spastorino Revert "Merge pull request #2194 from cldwalker/after_initialize"
This reverts commit 1983007, reversing
changes made to ae65c0b.
434bbb0
@spastorino
Ruby on Rails member

@cldwalker I've reverted it we should discuss this for master, do you mind to report this again for master

@cldwalker

@spastorino I've reported this again for master with #2236

@ttosch ttosch pushed a commit that referenced this pull request
@spastorino spastorino Revert "Merge pull request #2194 from cldwalker/after_initialize"
This reverts commit 1983007, reversing
changes made to ae65c0b.
cb82e17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 23, 2011
  1. @hmcfletch @cldwalker

    fix after_initialize edge case (close #2074 and close #2175)

    hmcfletch committed with cldwalker
    fix behavior when after_initialize is defined and a block is passed to Base.create
  2. @cldwalker
This page is out of date. Refresh to see the latest.
View
3 activerecord/lib/active_record/base.rb
@@ -505,8 +505,7 @@ def create(attributes = nil, options = {}, &block)
if attributes.is_a?(Array)
attributes.collect { |attr| create(attr, options, &block) }
else
- object = new(attributes, options)
- yield(object) if block_given?
+ object = new(attributes, options, &block)
object.save
object
end
View
15 activerecord/test/cases/base_test.rb
@@ -21,6 +21,7 @@
require 'models/person'
require 'models/edge'
require 'models/joke'
+require 'models/wholesale_product'
require 'rexml/document'
require 'active_support/core_ext/exception'
@@ -250,6 +251,20 @@ def test_initialize_with_invalid_attribute
end
end
+ def test_create_after_initialize_without_block
+ wp = WholesaleProduct.create(:wholesale => 10)
+ assert_equal(20, wp.msrp)
+ assert_equal(10, wp.wholesale)
+ end
+
+ def test_create_after_initialize_with_block
+ wp = WholesaleProduct.create do |w|
+ w.wholesale = 10
+ end
+ assert_equal(20, wp.msrp)
+ assert_equal(10, wp.wholesale)
+ end
+
def test_load
topics = Topic.find(:all, :order => 'id')
assert_equal(4, topics.size)
View
9 activerecord/test/models/wholesale_product.rb
@@ -0,0 +1,9 @@
+class WholesaleProduct < ActiveRecord::Base
+
+ after_initialize :set_prices
+
+ def set_prices
+ self.msrp = 2 * wholesale if wholesale
+ end
+
+end
View
5 activerecord/test/schema/schema.rb
@@ -676,6 +676,11 @@ def create_table(*args, &block)
t.references :wheelable, :polymorphic => true
end
+ create_table :wholesale_products, :force => true do |t|
+ t.integer :msrp
+ t.integer :wholesale
+ end
+
create_table :zines, :force => true do |t|
t.string :title
end
Something went wrong with that request. Please try again.