Search code examples
ruby-on-railshas-scope

Routing error with has_scope


I am getting the error below with has_scope. This seems like a really obvious and basic error but I just can't work it out. I'd really appreciate any help that can be offered.

I have got ActiveAdmin elsewhere on the site and I believe that uses it so we can assume the gem is operating correctly.

ActionController::RoutingError at /products
undefined method `has_scope' for ProductsController:Class

Model:

class Product < ActiveRecord::Base
    belongs_to :category

    # Scopes
    default_scope { order('end_date DESC') } 
    scope :upward_trending, -> { where( "status > ?", 100).order('end_date DESC') }
end

Controller:

class ProductsController < ApplicationController
    before_filter :authenticate_user!

    has_scope :upward_trending

    def product_params
        params.require(:product).permit(:name, :status)
    end

    def index
        @q = Product.search(params[:q])
        @products = apply_scopes(@q.result.page(params[:page]).per(5)).all
    end


    def show
    end

end

Routes:

resources :products, only: [:index]

Solution

  • Looking at the documentation of the gem has_scope[https://github.com/plataformatec/has_scope], it looks like you need to pass in :type as :boolean to has_scope method in the controller. This is applicable to the scopes that do not accept a parameter.

    has_scope :upward_trending, :type => :boolean