aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2013-03-28 05:14:00 -0700
committerCarlos Antonio da Silva <carlosantoniodasilva@gmail.com>2013-03-28 05:14:00 -0700
commit011c8ee4cb02b80d257de546ca11fd6c98ecb06b (patch)
treeac78c363b5afb881106308072e16141f95630fdf /activerecord
parent5533994b335560f2bf6b1dc6aef995b37c4657e0 (diff)
parent482f8c15b1d699c95bfbc3d836f674a09c0d9031 (diff)
downloadrails-011c8ee4cb02b80d257de546ca11fd6c98ecb06b.tar.gz
rails-011c8ee4cb02b80d257de546ca11fd6c98ecb06b.tar.bz2
rails-011c8ee4cb02b80d257de546ca11fd6c98ecb06b.zip
Merge pull request #9963 from obrie/fix/after_create_updates
Fix updates not working within after_create hooks
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/persistence.rb2
-rw-r--r--activerecord/test/cases/persistence_test.rb16
2 files changed, 17 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb
index f881778591..42cece3ad0 100644
--- a/activerecord/lib/active_record/persistence.rb
+++ b/activerecord/lib/active_record/persistence.rb
@@ -443,7 +443,7 @@ module ActiveRecord
real_column = db_columns_with_values[i].first
bind_attrs[column] = klass.connection.substitute_at(real_column, i)
end
- stmt = klass.unscoped.where(klass.arel_table[klass.primary_key].eq(id_was)).arel.compile_update(bind_attrs)
+ stmt = klass.unscoped.where(klass.arel_table[klass.primary_key].eq(id_was || id)).arel.compile_update(bind_attrs)
klass.connection.update stmt, 'SQL', db_columns_with_values
end
end
diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb
index a29189df05..ff06bd4f8b 100644
--- a/activerecord/test/cases/persistence_test.rb
+++ b/activerecord/test/cases/persistence_test.rb
@@ -296,6 +296,22 @@ class PersistencesTest < ActiveRecord::TestCase
assert_equal "Reply", topic.type
end
+ def test_update_after_create
+ klass = Class.new(Topic) do
+ def self.name; 'Topic'; end
+ after_create do
+ update_attribute("author_name", "David")
+ end
+ end
+ topic = klass.new
+ topic.title = "Another New Topic"
+ topic.save
+
+ topicReloaded = Topic.find(topic.id)
+ assert_equal("Another New Topic", topicReloaded.title)
+ assert_equal("David", topicReloaded.author_name)
+ end
+
def test_delete
topic = Topic.find(1)
assert_equal topic, topic.delete, 'topic.delete did not return self'