From 4f1d353b186ef01526a7b2a4117e869906571568 Mon Sep 17 00:00:00 2001 From: Marcel Molina Date: Wed, 5 Dec 2007 15:13:10 +0000 Subject: Document options and add examples for update_all. Closes #7990 [fearoffish] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8290 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/base.rb | 25 ++++++++++++++++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 54001561fb..e9957700d1 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Document options and add examples for update_all. Closes #7990 [fearoffish] + * Document options for update_counters. Closes #8091 [fearoffish] * Add documentation about the virtual attribute added by validates_confirmation_of and its behavior. Closes #8815 [JEG2, matt, kampers] diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index dad0573d88..0935af40fb 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -521,12 +521,27 @@ module ActiveRecord #:nodoc: id.is_a?(Array) ? id.each { |id| destroy(id) } : find(id).destroy end - # Updates all records with the SET-part of an SQL update statement in +updates+ and returns an integer with the number of rows updated. - # A subset of the records can be selected by specifying +conditions+. Example: - # Billing.update_all "category = 'authorized', approved = 1", "author = 'David'" + # Updates all records with details given if they match a set of conditions supplied, limits and order can + # also be supplied. # - # Optional :order and :limit options may be given as the third parameter, - # but their behavior is database-specific. + # ==== Options + # + # +updates+ A String of column and value pairs that will be set on any records that match conditions + # +conditions+ An SQL fragment like "administrator = 1" or [ "user_name = ?", username ]. + # See conditions in the intro for more info. + # +options+ Additional options are :limit and/or :order, see the examples for usage. + # + # ==== Examples + # + # # Update all billing objects with the 3 different attributes given + # Billing.update_all( "category = 'authorized', approved = 1, author = 'David'" ) + # + # # Update records that match our conditions + # Billing.update_all( "author = 'David'", "title LIKE '%Rails%'" ) + # + # # Update records that match our conditions but limit it to 5 ordered by date + # Billing.update_all( "author = 'David'", "title LIKE '%Rails%'", + # :order => 'created_at', :limit => 5 ) def update_all(updates, conditions = nil, options = {}) sql = "UPDATE #{table_name} SET #{sanitize_sql_for_assignment(updates)} " scope = scope(:find) -- cgit v1.2.3