Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 4 在关注点中定义的覆盖范围中调用super_Ruby On Rails 4 - Fatal编程技术网

Ruby on rails 4 在关注点中定义的覆盖范围中调用super

Ruby on rails 4 在关注点中定义的覆盖范围中调用super,ruby-on-rails-4,Ruby On Rails 4,我希望将一个附加查询链接到模型中的作用域上。范围在关注点中定义 module Companyable extend ActiveSupport::Concern included do scope :for_company, ->(id) { where(:company_id => id) } end end class Order < ActiveRecord::Base incl

我希望将一个附加查询链接到模型中的作用域上。范围在关注点中定义

module Companyable
    extend ActiveSupport::Concern

    included do
        scope :for_company, ->(id) {
            where(:company_id => id)
        }
    end
end


class Order < ActiveRecord::Base
    include Companyable

    # I'd like to be able to do something like this:
    scope :for_company, ->(id) {
        super(id).where.not(:status => 'cancelled')
    }
end
模块可压缩
扩展ActiveSupport::关注点
包括做
范围:适用于公司,->(id){
其中(:company_id=>id)
}
结束
结束
类顺序(id){
超级(id).where.not(:status=>“已取消”)
}
结束

然而,这可以理解地为类“订单”抛出了一个
名称错误:未定义的方法“for_company”

以下是我在本例中提出的解决方案:

与其使用
scope
,不如使用常规类方法,因为
scope
只是类方法的“语法糖”。当您需要使用
super
覆盖时,这更容易处理。在您的情况下,它将如下所示:

module Companyable
  extend ActiveSupport::Concern

  module ClassMethods
    def for_company(id)
      where(:company_id => id)
    end
  end
end



class Order < ActiveRecord::Base
  include Companyable

  def self.for_company(id)
    super(id).where.not(:status => 'cancelled')
  end
end
模块可压缩
扩展ActiveSupport::关注点
模块类方法
_公司的def(id)
其中(:company_id=>id)
结束
结束
结束
类顺序“已取消”)
结束
结束

你有没有找到这样做的方法?@60ty4bit对不起,nook,早些时候我遇到你的问题时,我正在努力解决这个问题。我已经发布了我的想法,以防对你有所帮助。