【问题标题】:How could I share scope method with other models using Rails 4 and mongoind我如何使用 Rails 4 和 mongoind 与其他模型共享范围方法
【发布时间】:2015-06-28 02:59:27
【问题描述】:

如何使用 Rails 4 和 mongoind 与其他模型共享范围方法

如果将scope 放入included do,则错误为

NoMethodError:未定义的方法“范围”为用户:类

如果将scope 放入module ClassMethods,则错误为

NoMethodError: 最近搜索::ClassMethods:Module 的未定义方法“范围”

代码 sn-p

class User
  include RecentlySearch
  include Mongoid::Document
  field :history, type: Array
end

截图 2

module RecentlySearch
extend ActiveSupport::Concern

module ClassMethods
  scope :hi, -> {p 'hi'}
end

included do
  scope :hi, -> {p 'hi'}

end

结束

【问题讨论】:

    标签: ruby-on-rails-4 mongoid


    【解决方案1】:

    scope 类方法来自Mongoid::Document 所以当你:

    include RecentlySearch
    

    目前还没有scope 方法。您需要先包含Mongoid::Document

    include Mongoid::Document
    include RecentlySearch
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-06
      • 1970-01-01
      相关资源
      最近更新 更多