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?

4

1 回答 1

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
于 2014-08-02T03:46:56.573 回答