有没有办法在我的范围或我的类方法中使用模型的方法?
我的联系人类中有这两种方法,定义联系人是否处于活动状态
def active
return no_of_activities_last_year > 0 ? true : false
end
def no_of_activities_last_year
time_range = (Time.now - 1.year)..Time.now
activities.where(:updated_at => time_range).count
end
然后我希望能够通过范围或类方法返回所有活动联系人,如下所示:
scope :active_contacts, lambda {where(:active => true)}
或者
def self.active_contacts
where(:active => true)
end
但是它们不起作用,因为活动不是数据库的一部分。
有什么聪明的方法可以通过这个吗?并且以可以与其他范围结合的方式更可取?