jhund / filterrific

Filterrific is a Rails Engine plugin that makes it easy to filter, search, and sort your ActiveRecord lists.
http://filterrific.clearcove.ca
MIT License
909 stars 124 forks source link

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

Open ConfusedVorlon opened 6 years ago

ConfusedVorlon commented 6 years ago

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

k4m1113 commented 6 years ago

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
    )
  }
ConfusedVorlon commented 6 years ago

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

k4m1113 commented 6 years ago

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?

mayzin00 commented 6 years ago

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

bastianwegge commented 5 years ago

@kamillamagna did you do any progress on this?