【发布时间】:2017-01-06 15:35:24
【问题描述】:
我有一个实例方法,它具有我想在查询中使用的逻辑。我的尝试没有奏效。在构建 where 子句时,我宁愿不复制 is_thing 中的逻辑。
class Foo < ActiveRecord::Base
def is_thing?
#... some logic
end
end
我试过了
Foo.where(is_thing?)
得到了
NoMethodError: 未定义的方法 `is_thing?'对于主:对象
【问题讨论】:
-
试试 rails
scope。 Referrence -
而调用实例方法的方式是通过对象:
Foo.where(Foo.new.is_thing?)
标签: ruby-on-rails activerecord ruby-on-rails-5