Using find method to find NOT matching
I'm trying to look up a User by group_id. My method looks kinda like this:
User.find(:all, :conditions => {:group_id => '90a39597a1aa3ec65fcc开发者_开发知识库f29ae05d9b6aefbfea6b', :id => !current_user.id})
The :id condition doesn't make sense because I don't know the syntax for this part. How would I find a user by group_id and :id which doesn't match the current_user.
Hope that makes sense.
Use the array/string variation of the conditions option:
User.all(:conditions => ["group_id = ? and id != ?", "90a39597a1aa3ec65fccf29ae05d9b6aefbfea6b", current_user.id])
The easiest way to do this, in my opinion, is to grab all the users with that group_id
and then remove the current user.
users = User.find_all_by_group_id('...')
users.delete(current_user)
You could do it via SQL but this just seems cleaner to me.
精彩评论