How to use OR condition in ActiveRecord query

后端 未结 5 1119
轻奢々
轻奢々 2020-12-09 02:38

I want to grab all the users that either have an email as the one supplied or the first and last name. So for example:

users = User.where(:first_name => \         


        
5条回答
  •  余生分开走
    2020-12-09 02:55

    Try this:

    users = User.where("(first_name = 'James' and last_name = 'Scott') or email = 'james@gmail.com'")
    

    To interpolate variables:

    users = User.where("(first_name = ? and last_name = ?) or email = ?", first_name, last_name, email)
    

提交回复
热议问题