1

Code from Controller + Error:

def search_conditions
    conditions = []
    if !params[:player_name].nil? && !params[:player_name].empty?
      conditions << ["lower(players.name) LIKE ?", "#{params[:player_name].downcase}%"]
    end
    conditions
end

def index
    @games = Game.joins([:player_one, :player_two]).where(search_conditions)
end

undefined method `%' for ["lower(players.name) LIKE ?", "test%"]:Array
Stack Trace:
activerecord (3.2.6) lib/active_record/sanitization.rb:121:in `sanitize_sql_array'
activerecord (3.2.6) lib/active_record/sanitization.rb:28:in `sanitize_sql_for_conditions'
activerecord (3.2.6) lib/active_record/relation/query_methods.rb:324:in `build_where'
activerecord (3.2.6) lib/active_record/relation/query_methods.rb:136:in `where'
app/controllers/game_controller.rb:5:in `index'

Looking at the stack trace, I search through the active record source and was able to find the line causing the issue in the sanitize_sql_array method:

statement % values.collect { |value| connection.quote_string(value.to_s) }

Now, I don't know enough about ruby to know exactly what that line does. I thought it initially had to do with the percent sign in the string, but removing it results in the same error still. I've also even tried constructing the string completely with creating a multidimensional array, but I still get the same error.

2
  • 1
    This doesn't answer your question, but I'd suggest changing if !params[:player_name].nil? && !params[:player_name].empty? to if params[:player_name].present? Basically, object.blank? returns true if it's nil or empty. object.present? returns true if object.blank? returns false. These are Rails methods, but it looks like you're working with Rails, so I thought I'd throw that out there. Commented Jul 23, 2012 at 19:08
  • @MichaelStalker Thanks! I didn't know about the .present? method. I'll definitely use that. Commented Jul 23, 2012 at 20:17

1 Answer 1

4

You're passing an array of arrays to where, rather than just an array. This means statement gets set as an array, but it expects a string (% on a string does string formatting, e.g: "hello %s" % "world" => "hello world").

conditions = []
conditions << ["lower(players.name) LIKE ?", "#{params[:player_name].downcase}%"]

  => [ ["lower(players.name) LIKE ?", "..."] ] 

Change:

conditions << ["lower(players.name) LIKE ?", "#{params[:player_name].downcase}%"]

to:

conditions.push "lower(players.name) LIKE ?", "#{params[:player_name].downcase}%"
Sign up to request clarification or add additional context in comments.

1 Comment

That worked! I thought I saw that done on another website somewhere but I guess they were wrong. I thought that's what the % sign did, but the way the statement was had me a little confused. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.