Skip to content

Commit

Permalink
Merge branch 'update_all'
Browse files Browse the repository at this point in the history
  • Loading branch information
miloops committed Jun 10, 2009
2 parents af9f9dd + 71528c2 commit d127831
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions activerecord/lib/active_record/base.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -839,26 +839,44 @@ def destroy(id)
# # Update all books that match our conditions, but limit it to 5 ordered by date # # Update all books that match our conditions, but limit it to 5 ordered by date
# Book.update_all "author = 'David'", "title LIKE '%Rails%'", :order => 'created_at', :limit => 5 # Book.update_all "author = 'David'", "title LIKE '%Rails%'", :order => 'created_at', :limit => 5
def update_all(updates, conditions = nil, options = {}) def update_all(updates, conditions = nil, options = {})
sql = "UPDATE #{quoted_table_name} SET #{sanitize_sql_for_assignment(updates)} " # sql = "UPDATE #{quoted_table_name} SET #{sanitize_sql_for_assignment(updates)} "


# scope = scope(:find)

# 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
# p sql

# connection.update(sql, "#{name} Update")
scope = scope(:find) scope = scope(:find)


select_sql = "" arel = arel_table
add_conditions!(select_sql, conditions, scope)
if conditions = construct_conditions(conditions, scope)
arel = arel.where(Arel::SqlLiteral.new(conditions))
end


if options.has_key?(:limit) || (scope && scope[:limit]) if options.has_key?(:limit) || (scope && scope[:limit])
# Only take order from scope if limit is also provided by scope, this # Only take order from scope if limit is also provided by scope, this
# is useful for updating a has_many association with a limit. # is useful for updating a has_many association with a limit.
add_order!(select_sql, options[:order], scope) arel = arel.order(construct_order(options[:order], scope)).take(construct_limit(options, 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 else
add_order!(select_sql, options[:order], nil) arel = arel.order(construct_order(options[:order], nil))
sql.concat(select_sql)
end end


connection.update(sql, "#{name} Update") arel.update(sanitize_sql_for_assignment(updates))
end end


# Destroys the records matching +conditions+ by instantiating each # Destroys the records matching +conditions+ by instantiating each
Expand Down

0 comments on commit d127831

Please sign in to comment.