Partial matches with find_by


#1

I have a form passing a parameter to a “show_search” action. Everything
goes fine, as long as i search for the exact title of the post. How do I
do a partial or “wild card” search? Code below:

def show_search
@posts = Post.find_all_by_title(params[:title])
end


#2

Jasbur wrote:

I have a form passing a parameter to a “show_search” action. Everything
goes fine, as long as i search for the exact title of the post. How do I
do a partial or “wild card” search? Code below:

def show_search
@posts = Post.find_all_by_title(params[:title])
end

Whilst I am far from an expert I think you need to use find options or
find_by_Sql

search_string = ‘’%’ + params[:title] + ‘%’’
Post.find(:all,
:conditions => [“title like ?”, search_string]
:order => ‘title’
)