I’m using Ruby on Rails 2.3.8 and I would like to get all the @products that haven’t been already added to the listed_products array.
For example, let’s say I’ve got the following code:
listed_products = ['1', '2', '3', '4', '5']
#Then, I would like to do something like SELECT * FROM products where id not in
#(listed_products), and save the result in @products
I know the above SQL syntax won’t work, but I just wanted to give you guys the idea of what I want to achieve.
Is there a “rails way” for doing this?
Thanks in advance!
Yes, you can do the following (Rails 2.3.x):
Or this in Rails 3.0.x:
listed_products = [1,2,3,4,5] Product.where("id NOT IN (?)", listed_products)