1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Extract id_for_database in the AR object

It is not connection's concern.
This commit is contained in:
Ryuta Kamizono 2020-05-31 09:13:29 +09:00
parent e93e45a502
commit c138e772e5
3 changed files with 9 additions and 6 deletions

View file

@ -44,6 +44,10 @@ module ActiveRecord
attribute_in_database(@primary_key)
end
def id_for_database # :nodoc:
@attributes[@primary_key].value_for_database
end
private
def attribute_method?(attr_name)
attr_name == "id" || super

View file

@ -203,9 +203,7 @@ module ActiveRecord
end
def id_value_for_database(value)
if primary_key = value.class.primary_key
value.instance_variable_get(:@attributes)[primary_key].value_for_database
end
value.id_for_database
end
def _quote(value)

View file

@ -193,13 +193,14 @@ module ActiveRecord
def quote_bound_value(value, c = connection)
if value.respond_to?(:map) && !value.acts_like?(:string)
quoted = value.map { |v| c.quote(v) }
if quoted.empty?
values = value.map { |v| v.respond_to?(:id_for_database) ? v.id_for_database : v }
if values.empty?
c.quote(nil)
else
quoted.join(",")
values.map! { |v| c.quote(v) }.join(",")
end
else
value = value.id_for_database if value.respond_to?(:id_for_database)
c.quote(value)
end
end