mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Moved update_record logic to relation.rb
This commit is contained in:
parent
a834b57b6e
commit
9693df61f0
2 changed files with 28 additions and 26 deletions
|
@ -428,23 +428,11 @@ module ActiveRecord
|
||||||
# Updates the associated record with values matching those of the instance attributes.
|
# Updates the associated record with values matching those of the instance attributes.
|
||||||
# Returns the number of affected rows.
|
# Returns the number of affected rows.
|
||||||
def update_record(attribute_names = @attributes.keys)
|
def update_record(attribute_names = @attributes.keys)
|
||||||
attributes_with_values = arel_attributes_with_values_for_update(attribute_names)
|
attributes_values = arel_attributes_with_values_for_update(attribute_names)
|
||||||
if attributes_with_values.empty?
|
if attributes_values.empty?
|
||||||
0
|
0
|
||||||
else
|
else
|
||||||
klass = self.class
|
self.class.unscoped.update_record attributes_values, id, id_was
|
||||||
column_hash = klass.connection.schema_cache.columns_hash klass.table_name
|
|
||||||
db_columns_with_values = attributes_with_values.map { |attr,value|
|
|
||||||
real_column = column_hash[attr.name]
|
|
||||||
[real_column, value]
|
|
||||||
}
|
|
||||||
bind_attrs = attributes_with_values.dup
|
|
||||||
bind_attrs.keys.each_with_index do |column, i|
|
|
||||||
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 || id)).arel.compile_update(bind_attrs)
|
|
||||||
klass.connection.update stmt, 'SQL', db_columns_with_values
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -56,16 +56,7 @@ module ActiveRecord
|
||||||
im = arel.create_insert
|
im = arel.create_insert
|
||||||
im.into @table
|
im.into @table
|
||||||
|
|
||||||
conn = @klass.connection
|
substitutes, binds = substitute_values values
|
||||||
|
|
||||||
substitutes = values.sort_by { |arel_attr,_| arel_attr.name }
|
|
||||||
binds = substitutes.map do |arel_attr, value|
|
|
||||||
[@klass.columns_hash[arel_attr.name], value]
|
|
||||||
end
|
|
||||||
|
|
||||||
substitutes.each_with_index do |tuple, i|
|
|
||||||
tuple[1] = conn.substitute_at(binds[i][0], i)
|
|
||||||
end
|
|
||||||
|
|
||||||
if values.empty? # empty insert
|
if values.empty? # empty insert
|
||||||
im.values = Arel.sql(connection.empty_insert_statement_value)
|
im.values = Arel.sql(connection.empty_insert_statement_value)
|
||||||
|
@ -73,7 +64,7 @@ module ActiveRecord
|
||||||
im.insert substitutes
|
im.insert substitutes
|
||||||
end
|
end
|
||||||
|
|
||||||
conn.insert(
|
@klass.connection.insert(
|
||||||
im,
|
im,
|
||||||
'SQL',
|
'SQL',
|
||||||
primary_key,
|
primary_key,
|
||||||
|
@ -82,6 +73,29 @@ module ActiveRecord
|
||||||
binds)
|
binds)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def update_record(values, id, id_was)
|
||||||
|
substitutes, binds = substitute_values values
|
||||||
|
um = @klass.unscoped.where(@klass.arel_table[@klass.primary_key].eq(id_was || id)).arel.compile_update(substitutes)
|
||||||
|
|
||||||
|
@klass.connection.update(
|
||||||
|
um,
|
||||||
|
'SQL',
|
||||||
|
binds)
|
||||||
|
end
|
||||||
|
|
||||||
|
def substitute_values(values)
|
||||||
|
substitutes = values.sort_by { |arel_attr,_| arel_attr.name }
|
||||||
|
binds = substitutes.map do |arel_attr, value|
|
||||||
|
[@klass.columns_hash[arel_attr.name], value]
|
||||||
|
end
|
||||||
|
|
||||||
|
substitutes.each_with_index do |tuple, i|
|
||||||
|
tuple[1] = @klass.connection.substitute_at(binds[i][0], i)
|
||||||
|
end
|
||||||
|
|
||||||
|
[substitutes, binds]
|
||||||
|
end
|
||||||
|
|
||||||
# Initializes new record from relation while maintaining the current
|
# Initializes new record from relation while maintaining the current
|
||||||
# scope.
|
# scope.
|
||||||
#
|
#
|
||||||
|
|
Loading…
Reference in a new issue