diff options
author | Tarmo Tänav <tarmo@itech.ee> | 2008-09-10 13:39:50 +0300 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2008-09-10 13:41:49 +0200 |
commit | 7c9851dbb6f841ffbf3ebd16e9f57c04319d3a39 (patch) | |
tree | adc55140a573c0c7cd99a405f2f593e721f1efb3 /activerecord/lib/active_record | |
parent | 14d1560e85d5c4795c21ddb00953cf55e0525ee3 (diff) | |
download | rails-7c9851dbb6f841ffbf3ebd16e9f57c04319d3a39.tar.gz rails-7c9851dbb6f841ffbf3ebd16e9f57c04319d3a39.tar.bz2 rails-7c9851dbb6f841ffbf3ebd16e9f57c04319d3a39.zip |
Support :limit on update_all so that has_many with :limit can be safely updated
Signed-off-by: Michael Koziarski <michael@koziarski.com>
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-x[-rw-r--r--] | activerecord/lib/active_record/base.rb | 20 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/mysql_adapter.rb | 4 |
3 files changed, 25 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index d7c67bc10d..fc6d762fcd 100644..100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -769,10 +769,24 @@ module ActiveRecord #:nodoc: # :order => 'created_at', :limit => 5 ) def update_all(updates, conditions = nil, options = {}) sql = "UPDATE #{quoted_table_name} SET #{sanitize_sql_for_assignment(updates)} " + scope = scope(:find) - add_conditions!(sql, conditions, scope) - add_order!(sql, options[:order], nil) - add_limit!(sql, options, nil) + + select_sql = "" + add_conditions!(select_sql, conditions, scope) + + if options.has_key?(:limit) || (scope && scope[:limit]) + # Only take order from scope if limit is also provided by scope, this + # is useful for updating a has_many association with a limit. + add_order!(select_sql, options[:order], scope) + + add_limit!(select_sql, options, scope) + sql.concat(connection.limited_update_conditions(select_sql, quoted_table_name, connection.quote_column_name(primary_key))) + else + add_order!(select_sql, options[:order], nil) + sql.concat(select_sql) + end + connection.update(sql, "#{name} Update") end diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb index aaf9e2e73f..8fc89de22b 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -153,6 +153,10 @@ module ActiveRecord "=" end + def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key) + "WHERE #{quoted_primary_key} IN (SELECT #{quoted_primary_key} FROM #{quoted_table_name} #{where_sql})" + end + protected # Returns an array of record hashes with the column names as keys and # column values as values. diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index f1d13698c3..c2a0fb72bf 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -523,6 +523,10 @@ module ActiveRecord "= BINARY" end + def limited_update_conditions(where_sql, quoted_table_name, quoted_primary_key) + where_sql + end + private def connect @connection.reconnect = true if @connection.respond_to?(:reconnect=) |