Ruby on rails 4 如何将范围查询链接到AND或替代AND?栏杆4
我试图将两个查询放在一个范围内-我不确定哪里出了问题。如蒙指教,不胜感激 我只想显示状态为“已接受”或“无”的用户 我写了下面的范围,并尝试了其他,但没有成功Ruby on rails 4 如何将范围查询链接到AND或替代AND?栏杆4,ruby-on-rails-4,Ruby On Rails 4,我试图将两个查询放在一个范围内-我不确定哪里出了问题。如蒙指教,不胜感激 我只想显示状态为“已接受”或“无”的用户 我写了下面的范围,并尝试了其他,但没有成功 Try scope:active_recruiters,->{wherestatus:['Accepted',nil]}您试图管理的查询如下所示: WHERE status = 'Accepted' OR status = 'IS NULL'; 您可能正在寻找类似的内容: WHERE status = 'Accepted' OR sta
Try scope:active_recruiters,->{wherestatus:['Accepted',nil]}您试图管理的查询如下所示:
WHERE status = 'Accepted' OR status = 'IS NULL';
您可能正在寻找类似的内容:
WHERE status = 'Accepted' OR status is null;
在你的代码中试试这个
scope :active_recruiters, -> {where(['status = ? OR status ?', 'Accepted', 'IS NULL'])}
更好的是,这个怎么样
scope :active_recruiters, -> { where :status, [nil, 'Accepted'] }
如果要使用或代替和用户AREL表格约束进行链接
scope = where( where(status: 'Accepted').arel.constraints.reduce( :and ).or( where(status: nil).arel.constraints.reduce( :and ) ) )
爱阿雷尔,活阿雷尔。非常感谢,我不知道你可以用这种方式访问arel。
scope = where( where(status: 'Accepted').arel.constraints.reduce( :and ).or( where(status: nil).arel.constraints.reduce( :and ) ) )