filterrific icon indicating copy to clipboard operation
filterrific copied to clipboard

Numeric searches trigger "undefined method `downcase' for <x>:Integer"

Open ConfusedVorlon opened this issue 7 years ago • 5 comments

test this in the live example type 1 in the student name field

expected: student1 should show (if they exist)

actual: exception and no records returned

by the time the query reaches search_terms(query), query is of Integer class and query.downcase triggers the error

I'm fixing this with

  	def self.search_terms(query)
  		#integer queries are sent through as integer class
  		query=query.to_s

but it should probably be resolved earlier in the stack

ConfusedVorlon avatar Jul 27 '17 13:07 ConfusedVorlon

The source code for the demo app shows that the search query field is only querying first name, last name, and email. To set it up to search by ID you would have to add a query on the student's ID column.

scope :search_query, lambda { |query|
    return nil  if query.blank?
    # condition query, parse into individual keywords
    terms = query.downcase.split(/\s+/)
    # replace "*" with "%" for wildcard searches,
    # append '%', remove duplicate '%'s
    terms = terms.map { |e|
      (e.gsub('*', '%') + '%').gsub(/%+/, '%')
    }
    # configure number of OR conditions for provision
    # of interpolation arguments. Adjust this if you
    # change the number of OR conditions.
    num_or_conditions = 3
    where(
      terms.map {
        or_clauses = [
          "LOWER(students.first_name) LIKE ?",
          "LOWER(students.last_name) LIKE ?",
          "LOWER(students.email) LIKE ?"
        ].join(' OR ')
        "(#{ or_clauses })"
      }.join(' AND '),
      *terms.map { |e| [e] * num_or_conditions }.flatten
    )
  }

k4m1113 avatar Sep 03 '17 04:09 k4m1113

Hi @kamillamagna ,

I think you're misunderstanding my bug. I'm not talking about searching by id, I'm talking about searching fields where there are numbers in the text content.

for example: name: Cod3rHero search term: 3

expected: should show 'Cod3rHero' actual: crash

ConfusedVorlon avatar Sep 05 '17 10:09 ConfusedVorlon

thank you for providing more info for a test case and o the Bachmanity! Confirming that this is a problem. @jhund I think I can fix this, but it will pop my cherry of contributing to open source. Can I make a pull request?

k4m1113 avatar Sep 05 '17 10:09 k4m1113

I just solved this problem and shared the solution at my gist. https://gist.github.com/mayzin00/6cbcedd6c6ce7f0eae605e56bd3370f8

mayzin00 avatar Sep 28 '17 11:09 mayzin00

@kamillamagna did you do any progress on this?

bastianwegge avatar Mar 20 '19 13:03 bastianwegge