On 5 April 2011 19:27, Michael Hanna <taomaili...@gmail.com> wrote:
> Do you know of a quick and dirty example? I understand the concept
> you're describing, but I don't know where I can find an example of the

OK, in your Todo model, create a named scope

named_scope :search, lambda { |s|
  options = { :order => :title }
  options[:conditions] = [ "upper(title) like :search_string", {
:search_string => "%#{s.strip.upcase}%" } ] unless s.blank?
  options
}

Then in your controller's index action

def index
  @todos = Todo.search(params[:search])

end

Since params is a Hash, it will return nil if it does not contain a key called
:search, so the named_scope won't set any conditions and just return
your Todos sorted alphabetically by title.

You can quickly test if it works by appending a search param to the URL,
eg http://localhost:3000/todos?search=code

BTW, my code may have errors as I just quickly wrote that up without
proper testing.  I hope this gives you a good idea of how it works.

Regards,
Franz

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to