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.
if !params[:player_name].nil? && !params[:player_name].empty?toif params[:player_name].present?Basically,object.blank?returnstrueif it's nil or empty.object.present?returnstrueifobject.blank?returnsfalse. These are Rails methods, but it looks like you're working with Rails, so I thought I'd throw that out there.