Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #2236 from cldwalker/after_initialize

Fix after_initialize and Base.create edge case
  • Loading branch information...
commit 64affc9384e9e8845b0204b94c003265442bc3e2 2 parents 14c0f0e + f5e4d13
@spastorino spastorino authored
View
3  activerecord/lib/active_record/base.rb
@@ -504,8 +504,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
13 activerecord/test/cases/base_test.rb
@@ -21,6 +21,7 @@
require 'models/person'
require 'models/edge'
require 'models/joke'
+require 'models/bulb'
require 'rexml/document'
require 'active_support/core_ext/exception'
@@ -260,6 +261,18 @@ def test_initialize_with_invalid_attribute
end
end
+ def test_create_after_initialize_without_block
+ cb = CustomBulb.create(:name => 'Dude')
+ assert_equal('Dude', cb.name)
+ assert_equal(true, cb.frickinawesome)
+ end
+
+ def test_create_after_initialize_with_block
+ cb = CustomBulb.create {|c| c.name = 'Dude' }
+ assert_equal('Dude', cb.name)
+ assert_equal(true, cb.frickinawesome)
+ end
+
def test_load
topics = Topic.find(:all, :order => 'id')
assert_equal(4, topics.size)
View
5 activerecord/test/models/bulb.rb
@@ -33,4 +33,9 @@ def self.new(attributes = {}, options = {}, &block)
end
class CustomBulb < Bulb
+ after_initialize :set_awesomeness
+
+ def set_awesomeness
+ self.frickinawesome = true if name == 'Dude'
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.