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.
You’re passing an array of arrays to
where, rather than just an array. This meansstatementgets set as an array, but it expects a string (%on a string does string formatting, e.g:"hello %s" % "world" => "hello world").Change:
to: