Controller helper method not receiving/defining parameters?

I have a pretty basic helper method which filters/searches through an existing active record result set:

module ReactEndpoints::ProductsHelper
    def filter_and_search_products(products, params)
        pry
        products = products.search_products(params[:search])
        # filtering etc
        return products
    end
end

The problem is, when I stick a pry call in there, and try to inspect products, I get:

[23] pry(#<#<Class:0x000055d249081710>>)> products
  CACHE InvProcure::Product Load (0.0ms)  SELECT "inv_products".* FROM "inv_products" WHERE "inv_products"."customer_id" = $1 ORDER BY description asc LIMIT $2  [["customer_id", 23], ["LIMIT", 11]]
  ↳ app/helpers/react_endpoints/products_helper.rb:3:in `filter_and_search_products'
  CACHE InvProcure::Product Load (0.0ms)  SELECT "inv_products".* FROM "inv_products" WHERE "inv_products"."customer_id" = $1 ORDER BY description asc LIMIT $2  [["customer_id", 23], ["LIMIT", 11]]
  ↳ app/helpers/react_endpoints/products_helper.rb:3:in `filter_and_search_products'
  CACHE InvProcure::Product Load (0.0ms)  SELECT "inv_products".* FROM "inv_products" WHERE "inv_products"."customer_id" = $1 ORDER BY description asc LIMIT $2  [["customer_id", 23], ["LIMIT", 11]]
  ↳ app/helpers/react_endpoints/products_helper.rb:3:in `filter_and_search_products'
  CACHE InvProcure::Product Load (0.0ms)  SELECT "inv_products".* FROM "inv_products" WHERE "inv_products"."customer_id" = $1 ORDER BY description asc LIMIT $2  [["customer_id", 23], ["LIMIT", 11]]
  ↳ app/helpers/react_endpoints/products_helper.rb:3:in `filter_and_search_products'
  CACHE InvProcure::Product Load (0.0ms)  SELECT "inv_products".* FROM "inv_products" WHERE "inv_products"."customer_id" = $1 ORDER BY description asc LIMIT $2  [["customer_id", 23], ["LIMIT", 11]]
  ↳ app/helpers/react_endpoints/products_helper.rb:3:in `filter_and_search_products'
  CACHE InvProcure::Product Load (0.0ms)  SELECT "inv_products".* FROM "inv_products" WHERE "inv_products"."customer_id" = $1 ORDER BY description asc LIMIT $2  [["customer_id", 23], ["LIMIT", 11]]
  ↳ app/helpers/react_endpoints/products_helper.rb:3:in `filter_and_search_products'
  CACHE InvProcure::Product Load (0.0ms)  SELECT "inv_products".* FROM "inv_products" WHERE "inv_products"."customer_id" = $1 ORDER BY description asc LIMIT $2  [["customer_id", 23], ["LIMIT", 11]]
  ↳ app/helpers/react_endpoints/products_helper.rb:3:in `filter_and_search_products'
  CACHE InvProcure::Product Load (0.0ms)  SELECT "inv_products".* FROM "inv_products" WHERE "inv_products"."customer_id" = $1 ORDER BY description asc LIMIT $2  [["customer_id", 23], ["LIMIT", 11]]
  ↳ app/helpers/react_endpoints/products_helper.rb:3:in `filter_and_search_products'
NameError: undefined local variable or method `products' for #<#<Class:0x000055d249081710>:0x000055d2490a2280>
Did you mean?  @products
from (pry):4:in `__pry__'

It seems to think products is undefined, but if I type @products I can see data.

class ReactEndpoints::ProductsController < ApplicationController
    def index
        @products = current_customer.products

        if params[:query].present?
            params[:query] = JSON.parse(params[:query])
            @products = helpers.filter_and_search_products(@products, params[:query])
        end

        page = params[:query] && params[:query][:page] || 1
        per = params[:query] && params[:query][:pageSize] || 10

        @products = @products.page(page).per(per)
    end
end

For whatever reason, it looks like pry doesn’t work properly within the context of these helper methods.

It’s generally considered bad practice to define namespaced classes in ruby (and particularly rails apps due to the autoloading), and there’s a plethora of literature out there detailing issues around structuring your code in this way.

# bad practice: this causes ReactEndpoints to be a class, which 
# much include an explicit module prefix when performing 
# constant lookup 
class ReactEndpoints::ProductsController < ApplicationRecord
end

# best practice: ReactEndpoints is now a module that can perform 
# constant lookup without a namespace prefix
module ReactEndpoints
  class ProductsController < ApplicationRecord
  end
end

Read up on namespaced classes and modules in Ruby for a better explanation.