在我的模型中,我想创建一个利用我的模型中的方法的范围。
我有以下模型方法:
def is_paired
! pairing_id.nil?
end
我想制作一个看起来像这样的范围
scope :paired, where(:is_paired => true)
但是当我这样做时,我得到一个错误:
SQLite3::SQLException: no such column: participants.is_paired: SELECT "participants".* F...
如何在模型中创建利用方法的范围???
答案 0 :(得分:1)
你做不到。基本上在where
,order
等范围内使用的方法。被“翻译”为sql查询。 Rails不知道如何将自定义方法转换为sql查询。
无论如何,您可以使用以下方法:scope :paired, where('pairing_id IS NOT NULL')
有关详细信息,请查看:Rails where condition using NOT NULL