Skip to main content

Benjamin Oakes

Photo of Ben Oakes

Hi, I'm Ben Oakes and this is my geek blog. Currently, I'm a Ruby/JavaScript Developer at Liaison. Previously, I was a Developer at Continuity and Hedgeye, a Research Assistant in the Early Social Cognition Lab at Yale University and a student at the University of Iowa. I also organize, ICRuby, OpenHack Iowa City, and previously organized NewHaven.rb. I have an amazing wife named Danielle Oakes.

Filtering for the month August, 2008. Clear


by Ben

I have an external hard drive set up on a Mac mini that I’d like to access over the network. I thought it would be easiest to use AFP, but I ran into permissions problems, receiving errors such as “You may need to enter the name and password for an administrator on this computer to change the item named ‘foo’.” even though everything in question should have been owned by me. This only occurreed when trying to copy files over to the shared volume. After doing some googling and fiddling, I gave up on AFP. SMB (which is configured under “Options” in Sharing Preferences) ended up working fine.

Smart Power Strips in OS X

by Ben

I recently purchased a Philips Smart Surge Protector (SPP3225WA/17). It’s nice because it can detect when an attached device is turned off and then turn off any other devices attached. This is especially useful when used with a computer because most peripherals are unusable when the computer is off, so there’s no sense in leaving them on.

It’s a great idea in theory, but I had problems using it with my Mac mini (OS X Leopard 10.5.4). Specifically, I want to use the built-in power scheduling (see “Energy Saver” -> “Schedule…” in System Preferences) to have the Mac turn off everything so we don’t have to remember to do so at the end of the day. Everything worked fine using the normal “Shut Down” command, but I didn’t want to have to make sure no one was doing anything every night. I wanted to use something like “Sleep”, but unfortunately, once the Mac went to sleep, it would turn off the attached external hard drives which would, in turn, wake the Mac back up. (It would also complain about not being properly ejected.) The most the Mac would stay asleep would be a couple of seconds.

But I found a solution! According to some information I found, there’s a way to change from using the default “sleep” and use something closer to what Windows users may know as “hibernate”. (It exists normally in OS X, but is only triggered by low battery.) To force it to always “hibernate” when sleeping (which is fine in the case of the Mac mini), you can run the following command at the command line:

sudo pmset -a hibernatemode 1

To reset this back to normal, run:

sudo pmset -a hibernatemode 3

Using this hibernate mode, the Mac no longer turns itself back on when the power to the external hard drives goes off. You can then set the Mac to sleep on whatever schedule works for you in System Preferences and everything else should turn off around 15 seconds after it starts going to sleep. (You’ll have to give it a little bit because it has to write the contents of memory to disk, which takes time.)

However, one problem still remains: OS X will still complain about the disks being removed improperly. To get around this, I wrote a small shell script which ejects them 5 minutes before the scheduled sleep time. You’ll need to tailor it to your setup, of course.

#!/usr/bin/env bash
# ~/bin/eject_external_drives

if [ -d "/Volumes/Time Machine" ]
  diskutil eject "/Volumes/Time Machine"

if [ -d "/Volumes/Storage" ]
  diskutil eject "/Volumes/Storage"

My crontab for this task looks like so:

55    11    *    *    *    /Users/ben/bin/eject_external_drives
50    11    *    *    *    /Users/ben/bin/eject_external_drives

(It tries twice before midnight.)

I haven’t used this extensively yet, but it seem to be working well in testing so far. Let me know if it works for you!

NULL (nil) in fixtures

by Ben

I was developing against SQLite for a while and didn’t notice that I had accidentally put "NULL" (i.e. as a string) in the fixtures. SQLite didn’t care about this, but MSSQL certainly did. Because I specified constraints on the column widths, I got the error:

ActiveRecord::StatementInvalid: DBI::DatabaseError: 22001 (8152) [FreeTDS][SQL Server]String or binary data would be truncated.

At first I thought you had to use nil in fixtures, but in fact, it’s actually NULL that you want.

Time in :conditions

by Ben

It took me quite a while to figure out that Time objects aren’t treated exactly as you might expect when using :conditions in a find or named_scope. From what I can tell, without extra help, Time objects are just treated as dates. This is fine if you’re checking conditions like I was outside the same day, but not if they are in the same day. This came up in writing a unit test for a model. My tests for the named scopes weren’t testing within the same day (i.e. only for values like 2.days.ago), so they passed, but other tests that expected to be able to set the open and close dates to a time within the same day would fail (confusingly).

So, it was incorrect to use the following, because Rails was treating them like a :date instead of a :datetime.

named_scope :active, lambda { now =; {:conditions => ['? > open_date and ? < close_date', now, now] } }

Instead, the following was used to make sure the time parts are included:

named_scope :active, lambda { now =; {:conditions => ['? > open_date and ? < close_date', now.to_s(:sql), now.to_s(:sql)] } }