Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #2194 from cldwalker/after_initialize

Fix after_initialize and Base.create edge case
  • Loading branch information...
commit 198300727de9cfae0e6b027c75aefc2488e19460 2 parents ae65c0b + 06c6252
@spastorino spastorino authored
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
Please sign in to comment.
Something went wrong with that request. Please try again.