I’ve been looking for a while and I can’t find a way to compose an
activerecord .where query that excludes certain ranges / values.
Like User.where(:id !=> [3,9,23], :active => 1)
help?
I’ve been looking for a while and I can’t find a way to compose an
activerecord .where query that excludes certain ranges / values.
Like User.where(:id !=> [3,9,23], :active => 1)
help?
You could always take the difference of two queries, if needed:
User.all - User.where(:id => [1,2,3])
If your list of ids to exclude is not long, you can also use SQL in a
where
argument:
User.where(“id != ? and id != ?”, 1, 2)
Phil
I’ve been looking for a while and I can’t find a way to compose an
activerecord .where query that excludes certain ranges / values.Like User.where(:id !=> [3,9,23], :active => 1)
User.where([“active = 1 AND id NOT IN (?)”, [3,9,23]])
If it were me, I’d create a scope on User named ‘active’ so you could
do…
User.active.where([“id NOT IN (?)”, [3,9,23]])
You could even create a “not_in” scope if you wanted to, but that might
be going a little too far…
https://github.com/ernie/meta_where looks like it has some pretty useful
extensions to the default .where
thanks!
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs