views:

25

answers:

2

I'm trying to find all users except for users with an id that is a member of an array exclude_ids

Here's what I have:

User.where("id != ?", exclude_ids)

This only works when exclude_ids has only 1 element. If it has more than one element, I get

this error:

ActiveRecord::StatementInvalid: SQLite3::SQLException: near ",": syntax error: SELECT     "users".* FROM       "users" WHERE     (id != 1,2)

I'd appreciate any help.

A: 

i am not confirm but you can Try this

User.where("id not in #{exclude_ids} ?")

+1  A: 

I havent used Rails 3 but its unsafe to use User.where("id in #{exclude_ids} ?").

On Rails 2.x you do it as follows

User.find(:all, :conditions => ["id NOT IN (?)", [1,2,3]])

Rails 3.x docs http://railsapi.com/doc/rails-v3.0.0.beta.3/classes/ActiveRecord/Base.html

Warren Noronha