Skip to content

@CDainMiller /gist:1661187
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Search Script
song.rb
def self.search(query)
if !query.to_s.strip.empty?
tokens = query.split.collect {|c| "%#{c.downcase}%"}
find_by_sql(["select s.* from songs s where #{ (["(lower(s.song_title) like ? or lower(s.song_lyrics) like ?)"] * tokens.size).join(" and ") } order by s.created_on desc", *(tokens * 2).sort])
else
[]
end
end
Once you’ve added the search method to your model, it’s even simpler to use.
search_controller.rb
def songs
@query = @params["query"]
@songs = Song.search(@query)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.