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/base.rb | |
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/base.rb')
-rwxr-xr-x[-rw-r--r--] | activerecord/lib/active_record/base.rb | 20 |
1 files changed, 17 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 |