Remove a 'where' clause from an ActiveRecord::Relation

Viewed 7585

I have a class method on User, that returns applies a complicated select / join / order / limit to User, and returns the relation. It also applies a where(:admin => true) clause. Is it possible to remove this one particular where statement, if I have that relation object with me?

Something like

User.complex_stuff.without_where(:admin => true)
5 Answers

This is an old question and this doesn't answer the question per say but rewhere is a thing that exists.

From the documentation:

Allows you to change a previously set where condition for a given attribute, instead of appending to that condition.

So something like:

Person.where(name: "John Smith", status: "live").rewhere(name: "DickieBoy")

Will output:

SELECT `people`.* FROM `people` WHERE `people`.`name` = 'DickieBoy' AND `people`.`status` = 'live';

The key point being that the name column has been overwritten, but the status column has stayed.

Related