我有一个带有范围和方法的模型,如下所示:
class Model < ActiveRecord::Base
scope :editable, where('updated_at > ? OR (updated_at IS NULL AND created_at > ?)', (Date.today - 3.days).beginning_of_day, (Date.today - 3.days).beginning_of_day)
def editable?
return (self.updated_at || self.created_at) > (Date.today - 3.days).beginning_of_day
end
end
我觉得我不应该在范围和方法中编写两次相同的逻辑。有什么办法可以避免这种情况吗?
我在 Rails 3.2 上
谢谢
最佳答案
不幸的是,目前没有简单的方法可以做到这一点。你能得到的最接近的是
def editable?
self.class.editable.exists? self
end
但是它会调用数据库来检查给定的记录是否存在,因此对于新记录或要更新的记录是无用的。
之所以没有这样的功能是因为这需要从 SQL/Arel 转换为 ruby 代码,这可能一点也不简单(因为其他模型可能涉及 JOIN 语句)。但是,如果您安装了 squeel gem,您应该能够编写:
class Model < ActiveRecord::Base
editable = -> {
(updated_at > (Date.today - 3.days).beginning_of_day) | ((updated_at == nil) & created_at > (Date.today - 3.days).beginning_of_day)
}
scope :editable, where &editable
def editable?
instance_eval &editable
end
end
这正是您所需要的。 (未经测试!)
更新:请注意,squeel gem 当前未维护,这可能很危险。
关于ruby-on-rails - rails 和 ActiveRecord : DRY use same logic in scope and boolean method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22814102/