Rails Model find where not equal

前端 未结 6 1375
孤街浪徒
孤街浪徒 2020-12-07 14:29

How can I find records in my database on a not equal condition? I have this now, but is there a fancy rails-speak way of doing it?

GroupUser.where(\'user_id         


        
6条回答
  •  再見小時候
    2020-12-07 15:08

    Rails 4:

    If you want to use both not equal and equal, you can use:

    user_id = 4
    group_id = 27
    GroupUser.where(group_id: group_id).where.not(user_id: user_id)
    

    If you want to use a variety of operators (ie. >, <), at some point you may want to switch notations to the following:

    GroupUser.where("group_id > ? AND user_id != ?", group_id, user_id)
    

提交回复
热议问题