JT Hopple LLC

Ruby

Mysql/Ruby Thread Blocking

JeremyProgramming, Ruby, Tips Digg!

It appears that the Mysql/Ruby library blocks all threads for the current process when executing a query. If you are running a multi-process web application this is not an issue, but for a multi-threaded application that has long-running queries this can be problematic.

I have a simple reporting service written with the Merb framework. In Merb you can make it handle multiple requests at once by setting use_mutex to false in the config/merb.yml file. This works great until you have a long running mysql query and it forcefully blocks ALL threads in the process. During this query your Merb app, which you thought was multi-threaded, is no longer multi-threaded.

List View Partial Pattern

JeremyDesign, Programming, Rails, Ruby, Tips Digg!

Most applications display lists of various things and they usually display these lists in slightly different ways. In my opinion, there isn't a one-size-fits-all solution to building these lists, but there is a pattern that seems to work pretty well. It consists of three parts: the list, the list-item, and the item. In addition to partitioning your lists into smaller pieces that are easier to deal with, it also makes it very easy to add both css styling and ajax behaviors to your lists.

Monkeycamp

JeremyPlugins, Rails, Ruby Digg!

I recently started working on a project that needs to integrate with Basecamp. 37signals provides a ruby script, basecamp.rb, to ease the use of their API, but it hasn't kept up. Specifically, they expanded the API to expose the time-tracking functionality in Basecamp.

Being fond of monkey patching I thought I'd create a Ruby on Rails plugin so I can easily add whatever methods I need without having to alter the actual basecamp.rb file. I'm sure I'll be adding more methods in the near future, but right now the plugin just contains one method for grabbing time entries.

You can grab this plugin from svn:

svn co http://svn.jthopple.com/plugins/monkeycamp

Or, install it directly from the root of a Rails project:

script/plugin install -x http://svn.jthopple.com/plugins/monkeycamp

Check out the documentation for more information.

Pass the Hash

JeremyProgramming, Rails, Ruby Digg!

Code readability is an undisputed contributor to increased programmer productivity. Ruby on Rails shines in this department by effectively using two main pieces of Ruby — the hash and the symbol. These two things are used all throughout Rails, but most importantly as method parameters. There are two keys to keep in mind when writing methods:

  1. When passing multiple data parameters to a method, pass a hash
  2. When telling a method how to behave, pass a symbol

Reliable Migrations

JeremyProgramming, Rails, Ruby Digg!

Ruby on Rails migrations rule! They're easy to get started with and have changed the way I approach database-driven application development. However, I was recently shocked when I used capistrano to deploy a new version of an application and one of the migrations exploded. It turns out that using ActiveRecords in your migrations can lead to unexpected results, but by following two simple guidelines you can ensure that your migrations will remain reliable throughout your application's life-cycle.