Skip to content

Commit

Permalink
Fix updates not working within after_create hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
obrie committed Mar 28, 2013
1 parent 57fbcc5 commit 482f8c1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/persistence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions activerecord/test/cases/persistence_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 482f8c1

Please sign in to comment.