Gavin Kistner
10/27/2004 2:58:00 AM
Great article!
One comment:
> def search
> terms = @params['search']['terms'].scan(/"(.*?)"|([^"\s]+)/).
> flatten.compact
> cond = "body LIKE '%#{terms[0]}%' "
> cond << terms[1..-1].map {|t| " AND body LIKE '%#{t}%'"}.join("")
> @results = Entry.find_all(cond)
> end
>
> But most of the scrappiness has to do with the piecing together of the
> SQL condition. Once all that is in place, the last line of the method
> body does everything that?s needed to prepare the environment for the
> view. (Note: readers are invited to send me ideas for cleaner ways to
> generate that multiple condition line!)
For the one bit I unerstand, how about:
def search
terms =
@params['search']['terms'].scan(/"(.*?)"|([^"\s]+)/).flatten.compact
@results = Entry.find_all terms.map{ |t| "body LIKE '%#{t}%'"
}.join( ' AND ' )
end