开发者

Using find method to find NOT matching

开发者 https://www.devze.com 2022-12-17 20:01 出处:网络
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 =>

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号