Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6440

Show
Ignore:
Timestamp:
03/17/07 15:48:47 (1 year ago)
Author:
bitsweat
Message:

Base.update_all :order and :limit options. Useful for MySQL updates that must be ordered to avoid violating unique constraints.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r6439 r6440  
    11*SVN* 
     2 
     3* Base.update_all :order and :limit options. Useful for MySQL updates that must be ordered to avoid violating unique constraints.  [Jeremy Kemper] 
    24 
    35* Remove deprecated object transactions.  People relying on this functionality should install the object_transactions plugin at http://code.bitsweat.net/svn/object_transactions.  Closes #5637 [Koz, Jeremy Kemper] 
  • trunk/activerecord/lib/active_record/base.rb

    r6420 r6440  
    502502      # A subset of the records can be selected by specifying +conditions+. Example: 
    503503      #   Billing.update_all "category = 'authorized', approved = 1", "author = 'David'" 
    504       def update_all(updates, conditions = nil) 
     504      # 
     505      # Optional :order and :limit options may be given as the third parameter, 
     506      # but their behavior is database-specific. 
     507      def update_all(updates, conditions = nil, options = {}) 
    505508        sql  = "UPDATE #{table_name} SET #{sanitize_sql_for_assignment(updates)} " 
    506         add_conditions!(sql, conditions, scope(:find)) 
     509        scope = scope(:find) 
     510        add_conditions!(sql, conditions, scope) 
     511        add_order!(sql, options[:order], scope) 
     512        add_limit!(sql, options, scope) 
    507513        connection.update(sql, "#{name} Update") 
    508514      end 
  • trunk/activerecord/test/base_test.rb

    r6348 r6440  
    570570  end 
    571571 
     572  if current_adapter?(:MysqlAdapter) 
     573    def test_update_all_with_order_and_limit 
     574      assert_equal 1, Topic.update_all("content = 'bulk updated!'", nil, :limit => 1, :order => 'id DESC') 
     575    end 
     576  end 
     577 
    572578  def test_update_many 
    573579    topic_data = { 1 => { "content" => "1 updated" }, 2 => { "content" => "2 updated" } }