Workaround the Memcached "undefined class/module" Bug

Posted by Chris Blackburn Thu, 31 May 2007 18:10:00 GMT

If you are experiencing the error: “undefined class/module MyClass” when fetching data from memcached, be assured that you are not alone. It is a known bug and the simplest way I know of to get around it is to reference the class or classes right before you retrieve data from the cache.

For example, if the following code causes the problem:

if not (genres = Cache.get(key))
  genres = Genre.find(:all, :condition => "platform_id = 1")
  Cache.put(key, genres, 60*60*24) # cache for 1 day
end

… then this code will work around it:

Genre
if not (genres = Cache.get(key))
  genres = Genre.find(:all, :condition => "platform_id = 1")
  Cache.put(key, genres, 60*60*24) # cache for 1 day
end

Notice the ‘Genre’ reference before the if statement. Some have reported success by using the ‘model’ statement within the controller, however that is deprecated. This workaround will get you going again.

Tags , , , , , ,  | 6 comments | no trackbacks

Long Ruby Strings :: The Here-Document

Posted by Chris Blackburn Wed, 23 May 2007 19:40:00 GMT

I am fairly anal about my code and the code that my firm (CBCI) produces. I like it to be formatted perfectly. It makes me cringe to even have a single extra space out of place, borking the indentation. Using tabs for indentation instead of spaces should be grounds for termination. :-D

In addition to perfect indentation, I don’t like scrolling to the right in my editor to view long lines of code. This can usually be handled properly by adding linefeeds. Consider the following example:

sql = %Q{
  SELECT
    users.id,
    users.username,
    users.email,
    users.created_at,
    users.activated_at,
    users.confirmed_at,
    affiliations.custom1 as affiliation_custom1,
    affiliations.custom2 as affiliation_custom2,
    affiliations.custom3 as affiliation_custom3,
    campaigns.name as affiliation_campaign_name
  FROM users
  LEFT OUTER JOIN affiliations ON affiliations.user_id = users.id
  LEFT OUTER JOIN campaigns ON campaigns.id = affiliations.campaign_id
}

There are several ways to handle this type of long string. First, for SQL statements, extra spaces in front of each line don’t matter, nor do the linefeeds. So the code above, using the ‘%Q’ method, would work just fine for building a string with spaces embedded. We could also use the lowercase ‘%q’ method which would wrap the string with single-quotes and prevent any escape sequences or interpolation of embedded expressions, which is fine since we don’t have any need for that in the above string. You could also wrap the quotation marks, single or double, around the string explicitly.

If you are working with a long character string, and are fussy about your code being formatted consistently, the indents will embed groups of spaces into your strings. As stated above, this technically doesn’t matter when considering SQL statements. In other cases where it does matter we use the Ruby HERE document as follows:

# first we extend the String class
class String
  def here_with_pipe
    lines = self.split("\n")
    lines.map! {|c| c.sub!(/\s*\|/, '')}
    new_string = lines.join(" ")
    self.replace(new_string)
  end
end

This strips off the leading whitespace and pipe character that gets embedded in the string using the HERE document like this:

  s = <<-end.here_with_pipe
    |This string will be concatenated
    |into a single line
    |containing no extra embedded spaces
    |and no line feeds.
  end

Isn’t Ruby cool!

Tags , , ,  | 1 comment | 1 trackback

Examining the Ruby Call Stack

Posted by Chris Blackburn Tue, 22 May 2007 22:52:00 GMT

Have you ever wanted to look at the call stack without raising an exception to do it?

caller.each {|c| puts c}

Tags , , ,  | no comments | no trackbacks