Is this the best way to do a "not in" in ActiveRecord 3.1?
I really like开发者_C百科 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.
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.
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.
精彩评论