Forum: Ruby on Rails Partial matches with find_by

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jasbur (Guest)
on 2006-06-14 02:27
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
880f96b24ef64881d9ee2e9f062d59a8?d=identicon&s=25 Glenn C. (glenn)
on 2006-06-14 03:51
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'
          )
This topic is locked and can not be replied to.