2

Here is my concern file: controllerconcerns.rb

require 'active_support/concern'

module Query_scopes
  extend ActiveSupport::Concern
  has_scope :title
end

Here is my controller I want to include it in: api_controller.rb

class ApiController < ApplicationController
  require 'concerns/controllerconcerns'
  include Query_scopes
  etc etc etc

Here is the error I'm getting:

undefined method `has_scope' for Query_scopes:Module

I have the has_scope gem installed and it works fine if I just say 'has_scope: scopename' within each individual controller I want it applied to... so how can I apply a few lines of 'has_scope' code to all my controllers?

infused
  • 24,000
  • 13
  • 68
  • 78
rikkitikkitumbo
  • 954
  • 3
  • 17
  • 38

1 Answers1

7

You should follow the naming convention for using concerns, and also include what you want in the included do block :)

eg.

module QueryScopes
  extend ActiveSupport::Concern

   included do
     has_scope :title
   end
end

and then:

class ApiController < ApplicationController
  include QueryScopes
end
derekyau
  • 2,936
  • 1
  • 15
  • 14
  • thanks! I think for sake of simplicity, in this case I don't even need include and modules... just the require and the 'included do' block will be fine! – rikkitikkitumbo Aug 02 '14 at 04:13
  • whoops... nevermind. It seems I HAVE to use include and define things in modules. Also, I had to use 'load' instead of require – rikkitikkitumbo Aug 02 '14 at 04:27
  • actually because rails loads the concerns by default, you don't really need to even require/load anything I don't think :), the above code should just work – derekyau Aug 02 '14 at 06:58