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