Permalink
Browse files

Merge pull request #9963 from obrie/fix/after_create_updates

Fix updates not working within after_create hooks
  • Loading branch information...
carlosantoniodasilva committed Mar 28, 2013
2 parents 5533994 + 482f8c1 commit 011c8ee4cb02b80d257de546ca11fd6c98ecb06b
Showing with 17 additions and 1 deletion.
  1. +1 −1 activerecord/lib/active_record/persistence.rb
  2. +16 −0 activerecord/test/cases/persistence_test.rb
@@ -443,7 +443,7 @@ def update_record(attribute_names = @attributes.keys)
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
@@ -296,6 +296,22 @@ def test_preserve_original_sti_type
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'

0 comments on commit 011c8ee

Please sign in to comment.