aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/query_methods.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2013-11-02 17:35:34 -0700
committerDavid Heinemeier Hansson <david@loudthinking.com>2013-11-02 17:36:45 -0700
commitf950b2699f97749ef706c6939a84dfc85f0b05f2 (patch)
treed6a8f95705fd23605db1c904acfffa158cfff7c8 /activerecord/lib/active_record/relation/query_methods.rb
parent4d7080f80c1ec4792730943b33b6eac2a1562b2a (diff)
downloadrails-f950b2699f97749ef706c6939a84dfc85f0b05f2.tar.gz
rails-f950b2699f97749ef706c6939a84dfc85f0b05f2.tar.bz2
rails-f950b2699f97749ef706c6939a84dfc85f0b05f2.zip
Added ActiveRecord::QueryMethods#rewhere which will overwrite an existing, named where condition.
Diffstat (limited to 'activerecord/lib/active_record/relation/query_methods.rb')
-rw-r--r--activerecord/lib/active_record/relation/query_methods.rb12
1 files changed, 12 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb
index 14d8671161..62c555f6d6 100644
--- a/activerecord/lib/active_record/relation/query_methods.rb
+++ b/activerecord/lib/active_record/relation/query_methods.rb
@@ -550,6 +550,18 @@ module ActiveRecord
end
end
+ # Allows you to change a previously set where condition for a given attribute, instead of appending to that condition.
+ #
+ # Post.where(trashed: true).where(trashed: false) #=> WHERE `trashed` = 1 AND `trashed` = 0
+ # Post.where(trashed: true).rewhere(trashed: false) #=> WHERE `trashed` = 0
+ # Post.where(active: true).where(trashed: true).rewhere(trashed: false) #=> WHERE `active` = 1 AND `trashed` = 0
+ #
+ # This is short-hand for unscope(where: conditions.keys).where(conditions). Note that unlike reorder, we're only unscoping
+ # the named conditions -- not the entire where statement.
+ def rewhere(conditions)
+ unscope(where: conditions.keys).where(conditions)
+ end
+
# Allows to specify a HAVING clause. Note that you can't use HAVING
# without also specifying a GROUP clause.
#