2

I have model A, B, and C which needs the same scope.

I found this question which mention about creating a module and include it:

require 'active_support/concern'

module Scopes
  extend ActiveSupport::Concern

  included do
    scope :disabled, where(:disabled => true)
  end

  module ClassMethods
   ...
  end
end

But then it doesn't explain on where to put and how to include.

I tried putting the code above in config/my_scopes.rb and include it in my application_controller.rb as follow:

class ApplicationController < ActionController::Base
  include Scopes
  ...
end

I got Routing Error saying this:

uninitialized constant ApplicationController::Scopes

I'm quite new to Rails so I'm not sure what cause it. Any help?

Thanks

Community
  • 1
  • 1
hrsetyono
  • 4,474
  • 13
  • 46
  • 80

1 Answers1

3

You should require file like require 'concerns/scopes.rb' in Rails 3.

Also you scope is for ActiveRecord, and you are including it in controller, that won't work I guess. You should do it like this:

module ActiveRecord
  module Base
    include Scopes
  end
end

I'm not sure where you should place this file (and will it work or not:)).

Or maybe:

class ActiveRecord
  include Scopes
end

And require this file in ApplicationController

zishe
  • 10,665
  • 12
  • 64
  • 103