Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore the ability to pass Active Record objects to update_all #25876

Merged
merged 1 commit into from
Jul 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 11 additions & 1 deletion activerecord/lib/active_record/sanitization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,17 @@ def expand_hash_conditions_for_aggregates(attrs)
def sanitize_sql_hash_for_assignment(attrs, table)
c = connection
attrs.map do |attr, value|
value = type_for_attribute(attr.to_s).serialize(value)
if value.is_a?(Base)
require "active_support/core_ext/string/filters"
ActiveSupport::Deprecation.warn(<<-WARNING.squish)
Passing `ActiveRecord::Base` objects to
`sanitize_sql_hash_for_assignment` (or methods which call it,
such as `update_all`) is deprecated. Please pass the id directly,
instead.
WARNING
else
value = type_for_attribute(attr.to_s).serialize(value)
end
"#{c.quote_table_name_for_assignment(table, attr)} = #{c.quote(value)}"
end.join(', ')
end
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/relations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2007,4 +2007,11 @@ def stubbed_connection.combine_bind_parameters(**kwargs)

assert_equal 2, posts.to_a.length
end

def test_update_all_can_receive_active_record_objects
assert_deprecated do
Comment.update_all(post_id: Post.first)
assert(Comment.all.all? { |c| c.post_id == Post.first.id })
end
end
end