You are here: Forums Ask a Rails expert Using like command in RoR...
Posted in Forums : Ask a Rails expert
Authority 0
Posting Rating 16
Sign in to rate this post
|
I’ve been working on this for a long time. I get this error when trying to work a sql statement like so: @names = CommonGivenName.find(:all, :conditions => [ “common_given_names.name LIKE ‘#{keyword}’” ]) I tried in this way it is working fine :conditions => [”common_given_names.name LIKE ?”, “’#{keyword}’”] But I need to construct with out using prepare statement. Please send mail to me this address |
Authority 37
Posting Rating 100
Sign in to rate this post
|
Why not just use the prepared statement? Just modify it like this: There are multiple examples in the Rails API documentation, for example here. HTH |
Authority 12
Posting Rating 96
Sign in to rate this post
|
Clemens is right. You should always use a prepared statement, otherwise you are setting yourself up for an SQL injection attack. Here’s a very good resource explaining SQL injection: http://www.unixwiz.net/techtips/sql-injection.html |
Authority 25
Posting Rating 98
Sign in to rate this post
|
Both Clemens and Jon give excellent advice. Also, note the ’%’ wildcard symbols that Clemens added. A LIKE statement is almost worthless without at least wildcarding the front, back or both ends of the search term. If you only needed exact matches then you should always opt for just using a standard ’=’ on an indexed field since LIKE statements can be a lot more expensive on most databases, causing a full table scan. |
Ask a Rails expert : How to use mephisto
Ask a Rails expert : How to use mephisto
Ask a Rails expert : will_paginate customization problem
Ask a Rails expert : BackgroundRB still wants 'development' environment...?
Ask a Rails expert : activescaffold, sql exception
Ask a Rails expert : Passing non-english chars in query string
Ask a Rails expert : Rails and 2D barcodes
Ask a Rails expert : apache giving proxy error
Ask a Rails expert : Custom Responses w/ 'extra' information...?
Ask a Rails expert : Log rotation in rails