Quick and dirty search in Rails

Ever wondered how to quick have a search function in your rails app?

Here is a super quick and maybe a lil dirty way to achieve so.

First step:

The view


 <p><%= form_tag("/searches", :method => "get") do %>

           <%= text_field_tag :keywords, params[:keywords] %> </p>

       <p>  <%= submit_tag("Search") %>

           <% end %>

       </p>

Second step:

search.rb model (app/models)

class Search < ActiveRecord::Base

  def self.find_results(search)

    if search

      Item.find(:all, :conditions => ['name LIKE ?', "%#{search}%"])

    else

      Item.find(:all)

    end

  end

end

Third step:

searches_controller.rb (app/controllers)

class SearchesController < ApplicationController

  def index

    @results = Search.find_results(params[:keywords])

  end

end

Fourth step:

index.html.erb (app/views/searches)

<% @results.each do |result| %>

             <p><%= result.name %></p>

Done :)

Just don’t forget to add   resources :searches, :controllers => ‘searches’ to your routes.rb!

Ruby headaches pills

If:

script/rails generate controller Hello

Is failing, try to remember that you have probably installed more than one version of rails in your history, so you might need to call:

rvm use 1.9.1 —default :)

"The future is distributed!"

Hire the hackers!

ckck:

Control panel at IBM. San Jose, California, circa 1958.
Photograph by Arnold Del Carlo.

ckck:

Control panel at IBM. San Jose, California, circa 1958.

Photograph by Arnold Del Carlo.

I still treasure my memories of two weeks spent in Buenos Aires almost 3 years ago, and this article has brought all of them back again :)

"Work smarter, play harder"

— Never got why we have to work hard… ;)

"Schmidt says definitively: the next generation of leaders and startups to emerge will involve mobile, local, and social. He explains that we’ve exhausted the limits of the PC as a platform, the future will be mobile first."

— Reinvent the future, everything else will follow.

(Source: TechCrunch)

Dublin sunset

Dublin sunset

Creative destruction

From Capitalism, Socialism and Democracy (New York: Harper, 1975) [orig. pub. 1942] by Joseph A. Schumpeter, pp. 82-85:

Capitalism, then, is by nature a form or method of economic change and not only never is but never can be stationary. And this evolutionary character of the capitalist process is not merely due to the fact that economic life goes on in a social and natural environment which changes and by its change alters the data of economic action; this fact is important and these changes (wars, revolutions and so on) often condition industrial change, but they are not its prime movers. Nor is this evolutionary character due to a quasi-automatic increase in population and capital or to the vagaries of monetary systems, of which exactly the same thing holds true. The fundamental impulse that sets and keeps the capitalist engine in motion comes from the new consumers, goods, the new methods of production or transportation, the new markets, the new forms of industrial organization that capitalist enterprise creates.