ruby-on-rails - rails 和 ActiveRecord : DRY use same logic in scope and boolean method

标签 ruby-on-rails ruby activerecord

我有一个带有范围和方法的模型,如下所示:

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/

相关文章:

ruby-on-rails - Rails加一以增加SQL值

ruby-on-rails - Postgres hstore 和 Rails sunspot solr

ruby - 将复杂文件拆分为哈希

ruby-on-rails - .distinct 不返回唯一条目

mysql - 如何查询日期范围 A 或日期范围 B 内的记录

ruby - 如何在 Ruby 中获取 linux 系统信息

ruby-on-rails - 为 has_many 关系创建方法?

ruby-on-rails - --skip-sprockets 和 --skip-javascript 仍然生成 app/assets/javascripts/application.js 吗?

ruby-on-rails - 更改 Rails 的数据库适配器

ruby - 检查(非子)进程是否正在运行的跨平台方法