Archive for November, 2008

Ruby on Rails: How to provide search with auto-completion

Saturday, November 29th, 2008

Preparation

For Rails 2.0 or higher, you will have to install a plug-in with the following command:

ruby script/plugin install auto_complete

Database

It is only assumed that you have a table with a text column. In this example, the table is called items and the column is called name.

Model

No changes are required for any model.

View

Your view must include the necessary JavaScript library:

<% javascript_include_tag :defaults %>

Your view must have a search form pointed at the controller for the items you want to search through:

<% form_tag('/items/search') do -%> 
  <%= text_field_with_auto_complete(:item, :name) %>
  <%= submit_tag('Search') %>
<% end -%>

Earlier versions of Rails will require the following:

<%= form_tag('/items/search') %>
  <%= text_field_with_auto_complete(:item, :name) %>
  <%= submit_tag('Search') %>
<%= end_form_tag %>

Controller

The controller must include the following line for auto-completion:

auto_complete_for :item, :name

The controller must also have a search method to handle eventual submission of the form. In this case, the search action delegates to index for rendering:

def search 
  @items = Item.find(:all, :conditions => ["name like (?)", '%' + params[:item][:name] + '%'])
  respond_to do |format|
    format.html { render(:action => ‘index’) }
    format.xml { render :xml => @items }
  end
end

Ruby on Rails: How to store and retrieve a MRU item in a cookie

Thursday, November 27th, 2008

It is convenient to display items that the user has visited recently, MRU (Most Recently Used) items.

Preparation

No special preparation is required.

Database

It is assumed only that you have a database table with an identifying column. In this example, the table is called items.

Model

No model changes are required.

Controller

To store an item in the cookie, add something like the following to your show method:

cookies[:item] = params[:id]

:item is simply a key that should be used consistently when setting or retrieving the value of the cookie. The key will be different for each controller. params[:id] retrieves the identifier of the shown element from the URL.

To retrieve an item from the cookie, add something like the following to your controller. Add it to a particular method that retrieves values for display, or add it to the application controller to make the MRU item always available for display.

if (nil == cookies[:item]) 
  @recentitem = nil
else
  begin
    @recentitem = Item.find(cookies[:item])
  rescue ActiveRecord::RecordNotFound
    @recentitem = nil
  end
end

View

You will need to display the item after it is retrieved by the controller.

<% if (@recentitem) %>
  <%= link_to(@recentitem.name, @recentitem) %>
<% end %>

Recession is Taboo

Wednesday, November 19th, 2008

In his conversation with Kai Ryssdal, Robert Samuelson eloquently expressed the need for economies to shrink once in a while. If only the Federal Reserve shared this realism…

I do think, though, that there are dilemmas here that we have not been very candid in acknowledging. You can’t find an economist who will stand up in public very often and say, “sometimes we need recessions.” Periodic small setbacks may be better than trying to prolong a business cycle for a large number of years, because we may discover that in a year or two, we suffer a much harsher and more dangerous recession.


Expectations

Saturday, November 1st, 2008

Liz commented as follows to my previous post:

…any thoughts on how to apply this to children? As parents, I think we need to expect certain things from them, to help them function in this world (being polite, not hitting, telling the truth, etc). So it would be tough to lower our expectations of them…

It has been my personal experience that children need clear and challenging expectations from their parents. Indeed, in the absence of clear expectations, children suffer fear and stress. They need to understand the consequences of failing to meet certain expectations. This is certainly validated by the full spectrum of parenting guidance, from academic (Dr. Spock or the American Pediatric Society) to popular (Dr. Phil or Supernanny).

The point is not to lower your expectations, but to set expectations that you can afford for them not to meet. We usually do this without even realizing it. To help our children develop normally and learn new skills, we put them in situations with some risk, but we minimize the risk. We take precautions, so that it’s not so bad even if things don’t go as expected.

  • We know it is foolish to give a toddler priceless fragile antiques to play with, even if we clearly say “I expect you to be careful!”
  • We don’t put our home or car in the hands of an seven-year-old and say “I expect you to turn off the iron when I leave!”
  • When we teach a five-year-old how to ride a bike, we do it with training wheels and protective gear in a low-traffic cul-de-sac, and we supervise them.
  • If my four-year-old needs lessons in being polite, I probably wouldn’t introduce him to my boss at performance review time. Rather, I would practice at home.
  • If my five-year-old is hitting other children, I would separate them (after putting all heavy and sharp instruments out of reach). I might play with my child myself for short and then longer periods, to encourage play without violence.

The problem is, parents sometimes set expectations they cannot afford not to have met. I don’t mean that they put enormous sums of money on the line or that they put the child’s life in danger, but they do put their own love, pride, patience and good nature on the line. The expectations may seem perfectly reasonable, but parents feel personally affronted, hurt or angry when they are not met, and they lash out with harsh words, blame and punishment. They instill fear or guilt. Furthermore, they rationalize such behavior by asserting that their only choice is between lowering expectations and lashing out. However, rationality and compassion demand that parents consider other reasons why expectations may not have been met…

  • In their limited personal experience, they seemed reasonable, but for the child, they are not.
  • They were not clearly articulated.
  • The child did not understand them.
  • The child understood them, but did not know how to meet them.
  • The child was somehow thwarted or distracted in a good-faith attempt to meet them.

By admitting these possibilities, parents sometimes decide that there are actually good reasons to lower or eliminate expectations. More often, they find creative ways to ensure expectations are met in the future. It is not easy, and multiple failures may precede success, but the compassionate and rational approach is the one that gradually builds success based on trust and security, not fear, guilt, or conditional love.

So, the answer is not to lash out in anger or to lower or eliminate your expectations in disappointment, but to replace disappointment and anger with compassion and rationality.