如何在ActiveRecord中为同一列执行多个where语句?

时间:2013-01-25 21:42:41

标签: ruby-on-rails-3 activerecord where multiple-instances

使用rails 3,如何在没有复杂内容或额外宝石的情况下执行多个where语句?

我已将此列“接受”并希望获取所有接受的值== false并接受== null

以下两个例子都失败了:

    @scholars = Scholars.where(:scholar_id => current_user.id).where(["accepted = ? or accepted = ?", true, null])

    @scholars = Scholars.where(:scholar_id => current_user.id).where(:accpeted => true).where(:accepted=> null)

2 个答案:

答案 0 :(得分:3)

尝试:

@scholars = Scholars.where(:scholar => current_user, :accepted => true).all + 
            Scholar.where(:scholar => current_user, :accepted => nil).all

你有没有为你的模特命名?#34;学者"?模型传统上是单数的......如果你正确地命名它,那应该是Scholar.where(...)

答案 1 :(得分:1)

正确的答案应该是

@profiles = Profile.where(:user_id => current_user.id, :accepted => [true, nil]).order(:accepted)