The update method presently creates an SQL statement and passes it to the connection adapter in one fell swoop. This requires a rewrite (usually mostly copy/paste) of the method if one wishes to modify the behavior in some way, such as for scoping or to limit the number of attributes that appear in the SET clause (see ticket #5961 or the acts_as_changed/acts_as_modified plugins at http://www.agilewebdevelopment.com/plugins/owner/119). This patch simply breaks the operation into smaller chunks that be easily modified.
From
# Updates the associated record with values matching those of the instance attributes.
# Returns the number of affected rows.
def update
connection.update(
"UPDATE #{self.class.table_name} " +
"SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false))} " +
"WHERE #{self.class.primary_key} = #{quote_value(id)}",
"#{self.class.name} Update"
)
end
To
def attributes_for_update
attributes_with_quotes(false)
end
def sql_for_update
"UPDATE #{self.class.table_name} " +
"SET #{quoted_comma_pair_list(connection, attributes_for_update)} " +
"WHERE #{self.class.primary_key} = #{quote_value(id)}"
end
# Updates the associated record with values matching those of the instance attributes.
# Returns the number of affected rows.
def update
connection.update(sql_for_update, "#{self.class.name} Update")
end
This concept can also be carried over to the insert and delete statements, of course.