newBike
newBike

Reputation: 15012

How could I share scope method with other models using Rails 4 and mongoind

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

Code snippet

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

snippert 2

module RecentlySearch
extend ActiveSupport::Concern

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

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

end

end

Upvotes: 0

Views: 58

Answers (1)

mu is too short
mu is too short

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

Related Questions