【问题标题】:Refactor class method to rails 4 scope将类方法重构为 Rails 4 范围
【发布时间】:2014-11-20 11:32:32
【问题描述】:

我有以下方法要移动到命名范围

def self.running_promotion
  Supplier.all.select { |s| s.has_running_promotion? == true }
end

不确定如何将lambda 与 rails 4 范围一起使用,或者是否可能。我试过了

scope :on_sale, -> { where({ |s| s.has_running_promotion? == true }) }

【问题讨论】:

    标签: ruby-on-rails scope named-scope


    【解决方案1】:

    如果has_running_promotion 是您表中的字段,您可以这样写:

    scope :on_sale, -> { where(has_running_promotion: true) }
    

    【讨论】:

    • 是的,但事实并非如此。 has_running_promotion? 是一种方法
    • 如果你没有has_running_promtion字段,添加你的has_running_promtion?与它无关,所以也许你应该添加你的has_running_promotion?方法来提问
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-11
    • 2011-08-19
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 1970-01-01
    • 2012-02-18
    相关资源
    最近更新 更多