aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation.rb
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2015-01-02 10:48:48 -0300
committerRafael Mendonça França <rafaelmfranca@gmail.com>2015-01-02 10:48:48 -0300
commit9598c9655f04b3bf6dfc245437ad5a09f23c792c (patch)
treef1e007f13264e8f19c00e3f014ab8fec10c79653 /activerecord/lib/active_record/relation.rb
parenta016f9b7282b6198cc5c7bdbba5f8aeb3017f3fb (diff)
parent5ef713c53c49b54615be9b5a400ac5810b404e76 (diff)
downloadrails-9598c9655f04b3bf6dfc245437ad5a09f23c792c.tar.gz
rails-9598c9655f04b3bf6dfc245437ad5a09f23c792c.tar.bz2
rails-9598c9655f04b3bf6dfc245437ad5a09f23c792c.zip
Merge pull request #11898 from prathamesh-sonpatki/patch-update
Changed ActiveRecord::Relation#update behavior so that it will work on Relation objects without giving id Conflicts: activerecord/CHANGELOG.md
Diffstat (limited to 'activerecord/lib/active_record/relation.rb')
-rw-r--r--activerecord/lib/active_record/relation.rb14
1 files changed, 13 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index cdafa6a50a..ab3debc03b 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -362,9 +362,21 @@ module ActiveRecord
# # Updates multiple records
# people = { 1 => { "first_name" => "David" }, 2 => { "first_name" => "Jeremy" } }
# Person.update(people.keys, people.values)
- def update(id, attributes)
+ #
+ # # Updates multiple records from the result of a relation
+ # people = Person.where(group: 'expert')
+ # people.update(group: 'masters')
+ #
+ # Note: Updating a large number of records will run a
+ # UPDATE query for each record, which may cause a performance
+ # issue. So if it is not needed to run callbacks for each update, it is
+ # preferred to use <tt>update_all</tt> for updating all records using
+ # a single query.
+ def update(id = :all, attributes)
if id.is_a?(Array)
id.map.with_index { |one_id, idx| update(one_id, attributes[idx]) }
+ elsif id == :all
+ to_a.each { |record| record.update(attributes) }
else
object = find(id)
object.update(attributes)