instance method
rewhere
Ruby on Rails edge
Since v4.1.16Signature
rewhere(conditions)
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` = true AND `trashed` = false
Post.where(trashed: true).rewhere(trashed: false)
# WHERE `trashed` = false
Post.where(active: true).where(trashed: true).rewhere(trashed: false)
# WHERE `active` = true AND `trashed` = false
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.
Parameters
-
conditionsreq
Source
# File activerecord/lib/active_record/relation/query_methods.rb, line 1130
def rewhere(conditions)
return unscope(:where) if conditions.nil?
scope = spawn
where_clause = scope.build_where_clause(conditions)
scope.unscope!(where: where_clause.extract_attributes)
scope.where_clause += where_clause
scope
end
Defined in activerecord/lib/active_record/relation/query_methods.rb line 1130
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::QueryMethods