From 638505b90f593c8ee48a060f378ed6a9c93442e1 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Sat, 6 Oct 2007 00:49:58 +0000 Subject: Send the correct INSERT statement when dealing with objects with only primary keys. Closes #9523 [tarmo] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7753 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/base.rb | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'activerecord/lib/active_record/base.rb') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 5e1185065e..068d9a5c45 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1968,9 +1968,11 @@ module ActiveRecord #:nodoc: # Updates the associated record with values matching those of the instance attributes. # Returns the number of affected rows. def update + quoted_attributes = attributes_with_quotes(false, false) + return 0 if quoted_attributes.empty? connection.update( "UPDATE #{self.class.table_name} " + - "SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false, false))} " + + "SET #{quoted_comma_pair_list(connection, quoted_attributes)} " + "WHERE #{connection.quote_column_name(self.class.primary_key)} = #{quote_value(id)}", "#{self.class.name} Update" ) @@ -1983,13 +1985,18 @@ module ActiveRecord #:nodoc: self.id = connection.next_sequence_value(self.class.sequence_name) end - self.id = connection.insert( + quoted_attributes = attributes_with_quotes + + statement = if quoted_attributes.empty? + connection.empty_insert_statement(self.class.table_name) + else "INSERT INTO #{self.class.table_name} " + "(#{quoted_column_names.join(', ')}) " + - "VALUES(#{attributes_with_quotes.values.join(', ')})", - "#{self.class.name} Create", - self.class.primary_key, self.id, self.class.sequence_name - ) + "VALUES(#{quoted_attributes.values.join(', ')})" + end + + self.id = connection.insert(statement, "#{self.class.name} Create", + self.class.primary_key, self.id, self.class.sequence_name) @new_record = false id -- cgit v1.2.3