Running ActiveRecord Tests

I have the occasion to need to extend ActiveRecord to support inserting and updating to nvarchar columns in MS SQL Server.  In order to accomplish this, I wanted to be able not only to run but also to extend the existing AR tests against SQL Server. So, I edited my ODBC data sources in /etc/odbc.ini and added a new connection underneath activerecord/test/connections for my database. Then, I needed to run the test/fixtures/db_definitions/sqlserver.sql script to have the tables in place. This wasn’t working out of the box with sqsh because of the format of the SQL script with semicolons as statement terminators. To solve this problem, I added the -L semicolon_hack=1 option to my sqsh command line, and the script ran no problem.

$ sqsh -U user -P password -S server -D database -L semicolon_hack=1 -i sqlserver.sql

Capistrano Lazy Evaluation

I setup a new environment in one of my Rails projects so that it would deploy to the same staging machine but to a different directory. Consequently, I did the following inside a task to override the default value:

set :deploy_to, "/var/apps/different/directory"

This caused the first half of the deployment to work fine (svn checkout to the correct directory, for example), but the symlink failed and then things went to hell after that.

I ran across this discussion on the Capistrano list, and decided to change the default setting of :deploy_to as follows:

set(:deploy_to) {"/var/apps/different/directory"}

Still problematic. But I noticed that references to #{shared_path} were correct by #{current_path} were not. That’s when I noticed this comment. That’s exactly what my problem was. I modified the deployment to set the mongrel_conf value as prescribed.

set(:mongrel_conf) {"#{current_path}/config/mongrel_cluster.yml"}

And it worked fine.

Sometimes in order to succeed, you have to give up

We went camping at Wallowa Lake, OR this summer.  What a beautiful place!  Of course, it took forever to get there from Bend, but it was well worth the trip.

As is typical when we go camping, Beth and I slept in our sleeping bag atop an air mattress.  This particular air mattress had been replaced several years ago because its predecessor started to leak right around the base of the circular baffles that join the top and the bottom of the air mattress together.

Well, apparently it was time for this air mattress to suffer the same structural defect.  I awoke one morning to find my hip touching the ground and realized I would need to fix this situation if I wanted a good night’s sleep going forward.

In the past, I had been able to super-inflate the mattress and use soapy water to spot the leak.  However, this leak was not cooperating — I would have to get more serious if I wanted to sleep well for the rest of the trip.  So, I took the air mattress to the lake, figuring that if I submerged the mattress, it would yield its leak(s) in the form of air bubbles.  After twenty minutes of pushing, prodding, and otherwise cajoling the mattress into giving up the secret location of the escaping air, I was defeated.  I knew it was there, but I couldn’t find the leak.

I was resigned to several more nights of subpar sleep.  I gave up.

What do you do when you are in a beautiful lake in the afternoon with an air mattress in front of you with the sun heading toward the horizon, much like your summer is sunsetting into autumn?   You jump on your air mattress and soak up some of summer’s final rays — that’s what you do!

It was a split-second after I landed on the air mattress that the telltale WHOOSH from the leak exposed its location.  I mapped the breach immediately by counting the x and y baffle “coordinates,” took the mattress back to camp, let it dry, and finally fixed the leak.

I slept just fine that night.

Finding :last

Rails makes it easy to find the first row of a query:


But what if you want the last one? It’d be great to be able to go:


… especially if you could pass in conditions, etc.

Well, this isn’t general purpose, but it tends to get you the last one that was created. Can be useful in testing circumstances:

last_order = Order.find(:first, :conditions => 'id = (select max(id) from orders)')

created_at and updated_at in fixtures

I’ve been working on some code that gets a list of “old” orders based on the created_at value for an order. Of course, I wrote a test using a fixture that gets the list of old orders and makes assertions about it. I was not as rigorous as I should have been and simply checked to make sure the correct count of orders was returned and went along my merry way.

Today the test broke when I made some other changes. During my investigation, I printed out the contents of the orders and discovered the created_at and updated_at values were being set to all zeros. OK, that would explain why they’re considered “old” if they’re from “the beginning of time.” But I still wanted to be able to put values in for created_at that would cause an order to be too new to be included in the query. So, I figured I’d do something like this:

created_at: <%= %>

No workie; still zeros. Well, thanks to this blog entry, I was able to get it right:

created_at: <%= %>

Rails REST testing using XML

One of the things that has been bugging me about my REST interfaces is that, although they are thoroughly tested in functional tests with all the GETs and POSTs and PUTs (and occasionally DELETEs), it just isn’t quite the same as literally POSTing the XML.

So, this morning I took the time to figure out a way to do this. It turns out that with an integration test, it’s quite easy. It’s also probably the Right Place™ to do this.


class RestXmlTest < ActionController::IntegrationTest
  fixtures :model_fixture

  # Test creating a new resource by actually POSTing the XML.
  def test_create_resource
    post "/path_to_resource.xml",
      "<resource><attribute_1>attribute value</attribute_1>...</resource>",
      {:content_type => "application/xml"}

    assert_response 201

Constant crisis

In my Rails application, I have a file in


On my development machine (OS X), I am able to reference items in this file/module using the prefix “PCM::WebServices::Constants” without having to require the file. Once deployed on Linux, however, this approach broke and I had to explicitly

require 'pcm/web_services/constants'

(or any other file I needed from the lib/pcm directory) for things to work. I suspected it had something to do with case sensitivity on Linux as opposed to not-so-case-sensitivity on OS X. I was wrong.

Upon further investigation, I find I can easily reproduce the problem, although I do not yet have an explanation.  The solution to the problems are (so far):

1) Explicitly require ‘pcm/web_services/constant’ wherever I need it.  No biggie, but I prefer the “less is more” philosophy where Rails automagically loads the constant without the explicit require.

More possible solutions coming soon…

—>8— cut here —>8—

1) Create a new Rails app, e. g. $ rails constant

2) Edit the environment.rb file to add your own module with
configuration constant default values with the idea you could
“override” these by specifying them in the specific environment file:

At the end of config/environment.rb, add:

module AppConstants
  IMPORTANT_VALUE = "x" unless defined?(AppConstants::IMPORTANT_VALUE)

3) Create a Ruby module in the file lib/app_constants/list.rb, noting
the “top level” name of the module matches the one specified in the
environment file:

module AppConstants
  module List
    LIST_CONSTANT = "list_constant"

3) Fire up the console and probe the constant values:

Loading development environment.

>> AppConstants::List::LIST_CONSTANT
=> "list_constant"
>> AppConstants::IMPORTANT_VALUE
=> "x"

4) Quit from the console and add development-specific value for the
constant defined in the environment file:

At the end of config/environments/development.rb, add:

module AppConstants
  IMPORTANT_VALUE = "definitely_not_x"

5) Fire up the console again and probe the constant values:

Loading development environment.

>> AppConstants::List::LIST_CONSTANT
NameError: uninitialized constant
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/
active_support/dependencies.rb:263:in `load_missing_constant'
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/
active_support/dependencies.rb:452:in `const_missing'
from (irb):1
>> AppConstants::IMPORTANT_VALUE
=> "definitely_not_x"
>> AppConstants::List::LIST_CONSTANT

NameError: uninitialized constant
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/
active_support/dependencies.rb:263:in `load_missing_constant'
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.4.2/lib/
active_support/dependencies.rb:452:in `const_missing'
from (irb):3