cbmeeks
cbmeeks

Reputation: 11420

Is this the best way to do a "not in" in ActiveRecord 3.1?

I really like being able to:

Product.where(:id => [40,41,42])

Which generates a nice where id in (40,41,42)

However, I could only figure out how to do the inverse as:

Product.where("id not in (?)", [40,41,42])

Is there a cleaner way?

Thanks.

Upvotes: 1

Views: 74

Answers (2)

Tilo
Tilo

Reputation: 33732

you can do something like this:

Product.all.delete_if{|x| [41,42,43].include? x.id }

==> Array with all products excluding the ones with the given ids.

Upvotes: 1

Ryan Bigg
Ryan Bigg

Reputation: 107728

Nope, that's how you do it. There is no shorter way, at least with vanilla ARel. You may find something within meta_where that could do it though.

Upvotes: 1

Related Questions