2

I'm trying to accomplish a multiple word searching in a quotes database using Ruby, ActiveRecord, and MySQL. The way I did is shown bellow, and it is working, but I would like to know if there a better way to do.

# receives a string, splits it in a array of words, create the 'conditions'
# query, and send it to ActiveRecord
def search
    query = params[:query].strip.split if params[:query]
    like = "quote LIKE "
    conditions = ""
    query.each do |word|
        conditions += (like + "'%#{word}%'")
        conditions += " AND " unless query.last == word
    end
    @quotes = Quote.all(:conditions => conditions)
end

I would like to know if there is better way to compose this 'conditions' string. I also tried it using string interpolation, e.g., using the * operator, but ended up needing more string processing. Thanks in advance

4 Answers 4

9

First, I strongly encourage you to move Model's logic into Models. Instead of creating the search logic into the Controller, create a #search method into your Quote mode.

class Quote
  def self.search(query)
    ...
  end
end

and your controller becomes

# receives a string, splits it in a array of words, create the 'conditions'
# query, and send it to ActiveRecord
def search
  @quotes = Quote.search(params[:query])
end

Now, back to the original problem. Your existing search logic does a very bad mistake: it directly interpolates value opening your code to SQL injection. Assuming you use Rails 3 you can take advantage of the new #where syntax.

class Quote
  def self.search(query)
    words = query.to_s.strip.split
    words.inject(scoped) do |combined_scope, word|
      combined_scope.where("quote LIKE ?", "%#{word}%")
    end
  end
end

It's a little bit of advanced topic. I you want to understand what the combined_scope + inject does, I recommend you to read the article The Skinny on Scopes.

4
  • Would be nice if you demonstrated mysql's full text searching in the examples Mar 11, 2011 at 19:56
  • 2
    I'd recommend to_s.downcase.strip.split.uniq to fold to lower-case and remove duplicate words. Mar 12, 2011 at 5:17
  • I'm using Rails 2.3.5, but anyway, the answer helped a lot. Thanks.
    – Eduardo
    Mar 12, 2011 at 14:37
  • Rails 5 CHANGE: replace 'scoped' with 'all'. Been using this helpful answer for years.
    – jpw
    Oct 19, 2020 at 23:32
3

MySQL fulltext search not working, so best way to do this:

class Quote
  def self.search_by_quote(query)
    words = query.to_s.strip.split
    words.map! { |word| "quote LIKE '%#{word}%'" }
    sql = words.join(" AND ")
    self.where(sql)
  end
end
1

The better way to do it would be to implement full text searching. You can do this in MySQL but I would highly recommend Solr. There are many resources online for implementing Solr within rails but I would recommend Sunspot as an entrance point.

1
  • Solr is indeed a good choice, but is a quite complex library. There are cases where you don't actually need to add a Solr dependency and it is perfectly fine to try such kind of query against MySQL. I believe in this case the user can highly benefit from refactoring his code without necessary taking the step to try Solr. Mar 11, 2011 at 17:11
0

Create a FULLTEXT index in MySQL. With that, you can leave string processing to MySQL.

Example : http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.