kisiel kisiel - 1 year ago 56
Ruby Question

Undefined method `text_search' for #<Array:0x00000005876838>

I'm new to Rails and I'm trying to do a search for my application.

I use

, before I made pagination. Search and pagination works very well, but one place I get an error:

undefined method `text_search' for #

Can someone help me and explain an error?
I tried to look for answers, but I do not understand this error in my situation.

My users controller:

require 'will_paginate/array'

class UsersController < ApplicationController
before_action :set_agent, only: [:index, :new, :create] # For nested /users/:user_id/users
before_action :authenticate_user!, only: [:show, :edit, :update]
before_action :authenticate_admin!, only: [:accept_agent]

respond_to :js, :only => [:users_list, :admins_list, :locked_list, :agents_list]

def index
users = filter_by_params(params, @agent)
@users = users
@users = @users.text_search(params[:query]).paginate(:page => params[:page], :per_page => 10)

and my

include PgSearch
pg_search_scope :search_name, :associated_against => {
:orders => [:client_name]}

def self.text_search(query)
if query.present?

Answer Source

You have two problems.

First, filter_by_params is giving you an array (presumably of users).

On that result you are trying to call text_search, but that is a class method as you've defined it, which means (as the comments have mentioned), that it would be called via 'User.text_search(..)' and not through a specific user object (which would be an instance method). I suggest some google research on the difference, one place to start would be:

Presumably you want to limit the users that you 'paginate' through the filter_by_params and then the text_search, as far as I can guess, though it's not clear from your question or code.

First step would probably be to deal with each user in the array at a time, something like:

users = filter_by_params(params, @agent)
users.each { |user|
   next if user.text_search(...)   # After rewriting this as an instance method

I can't tell if you are attempting to do pagination in the filter_by_params as well. You can only use the will_paginate gem on an ActiveRecord, so if you want to use that you'll need to figure out what your filtering is and do it through an ActiveRecord. This will probably be more efficient anyways.