Reputation: 15012
How could I share scope method with other models using Rails 4 and mongoind
if put scope
in included do
, the error was
NoMethodError: undefined method `scope' for User:Class
if put scope
in module ClassMethods
, the error was
NoMethodError: undefined method `scope' for RecentlySearch::ClassMethods:Module
class User
include RecentlySearch
include Mongoid::Document
field :history, type: Array
end
module RecentlySearch
extend ActiveSupport::Concern
module ClassMethods
scope :hi, -> {p 'hi'}
end
included do
scope :hi, -> {p 'hi'}
end
end
Upvotes: 0
Views: 58
Reputation: 434795
The scope
class method comes from Mongoid::Document
so when you:
include RecentlySearch
there is no scope
method yet. You need to include Mongoid::Document
first:
include Mongoid::Document
include RecentlySearch
Upvotes: 1