Why is my code in a loop?

January 24, 2007 Link to post  Permalink

Today, I wrote some code that looked a little bit like this:

 1lass Model < ActiveRecord::Base
 2
 3  ...
 4
 5  def update
 6    RAILS_DEFAULT_LOGGER.info "Update Called"
 7    # Read some data from wherever
 8    # Set some properties on my object
 9    save!
10  end
11end

Imagine my surprise when my log output was a long stream of Update Called lines.

What happened here is that save is implemented as a call to update when the object already exists.

Inadvertently picking a method name that is the same as an important Rails method can be hard to track down. I already had my logging in there, so I wasn’t left wondering why the call never completed.