update_allã¡ã½ããã£ã¦ä¾¿å©ã ãªã¼
where()ãªã©ã§å¿
è¦ãªè¤æ°ã¬ã³ã¼ããæ½åºãã¦ããã¦ãä¸æ¬ã§å¤å¤æ´ã§ãã¾ãã
ï¼ActiveRecord::Relationã®ã¡ã½ããã¨ãã¦å®è£
ããã¦ãï¼
ãã¾ã¾ã§sqlç´æ¸ããã¦ãããã(´ã»Ïã»ï½)
ãã®åã¯ãeachãã¬ã³ã¼ãåå®è¡ãã¦å¤æ¸ãæãã¦ããããããªããããã(´ï¼Ïï¼Ïï¼ï½)
å¿åé²çã«ãªãã¡ã¬ã³ã¹ããã¥ã¢ã«ã®ãµã³ãã«ãè²¼ãä»ãã¾ãã
# Update all customers with the given attributes Customer.update_all :wants_email => true # Update all books with 'Rails' in their title Book.update_all "author = 'David'", "title LIKE '%Rails%'" # Update all avatars migrated more than a week ago Avatar.update_all ['migrated_at = ?', Time.now.utc], ['migrated_at > ?', 1.week.ago] # Update all books that match conditions, but limit it to 5 ordered by date Book.update_all "author = 'David'", "title LIKE '%Rails%'", :order => 'created_at', :limit => 5 # Conditions from the current relation also works Book.where('title LIKE ?', '%Rails%').update_all(:author => 'David') # The same idea applies to limit and order Book.where('title LIKE ?', '%Rails%').order(:created_at).limit(5).update_all(:author => 'David')