Boosting ActiveRecord Performance

Posted about 1 year back at igvita.com

ActiveRecord is arguably one of the best ORM wrappers out there. And as any Rails developer will likely tell you, it’s also the backbone of the entire RoR architecture. Having said that, the flexibility comes with a trade off - you have a intermediary that is responsible for constructing the queries, hiding the database logic, converting data types, and the list goes on. All of the steps consume both time and resources, and while find_by_sql is powerful, it is often not enough. Thankfully, while flipping through ‘Pro Active Record‘ by Kevin Marshall, Chad Pytel and Jon Yurek I stumbled across a useful tip: ActiveRecord exposes a collection of methods which allow us to bypass all of its middleman functions and work with the raw SQL connection directly.

Working with low-level CRUD

If you look at the source of ActiveRecord you’ll quickly realize that all of its functionality boils down to a few simple methods: connection.insert, connection.update, connection.select_all and connection.delete. Issuing statements through any one of these methods requires a full SQL query - ActiveRecord is no longer responsible for verification. Essentially, we are bypassing everything ActiveRecord brings to the table, short of establishing a communications link to the database.

Bypassing ActiveRecord

Let’s establish a standalone connection to a test database and run our CRUD queries:

> ar-raw.rb

require 'active_record'
 
class DBConn < ActiveRecord::Base
  establish_connection(
      :adapter => "mysql",
      :database => "test",
      :username => "user",
      :password => "pass",
      :host => "localhost"
  )
end
 
# Return all documents; do not convert into ActiveRecord objects, keep as a hashmap
docs = DBConn.connection.select_all("select * from documents")
docs.each do |record|
  puts "#{record['id']} :: #{record['title']}"
end
 
# Insert a document
new_id = DBConn.connection.insert("insert into documents(id, group_id, date_added, title, content) values ('5', '1', '2007-10-26 12:00:00', 'Story title', 'Text')")
puts "\nNew document id: #{new_id}"
 
# Update a document
status = DBConn.connection.update("update documents set title = 'New title' where id = 5")
puts "\nUpdate status: #{status}"
 
# Check the updated document
p DBConn.connection.select_all("select * from documents where id = 5")
 
# Delete the update document (returns 0)
status = DBConn.connection.delete("delete from documents where id = 5")
puts "\nDelete status: #{status}\n"
 
# Check the table, once again.
docs = DBConn.connection.select_all("select * from documents")
docs.each do |record|
  puts "#{record['id']} :: #{record['title']}"
end

Executing the above code on a sample MySQL database with four document entries produces:

1 :: test one
2 :: test two
3 :: another doc
4 :: doc number four

New document id: 5

Update status: 1
[{”title”=>”New title”, “group_id”=>”1″, “id”=>”5″, “content”=>”Text”, “date_added”=>”2007-10-26 12:00:00″}]

Delete status: 1

1 :: test one
2 :: test two
3 :: another doc
4 :: doc number four

Working with low level SQL is definitely a lot more involved, but on the upside, it can also provide significant performance increases. If you are moving lots of data, need access to engine specific functions, or going for straight throughput, it’s not something you should overlook. Speed freaks: custom Mongrel handlers with raw ActiveRecord connections!

, , , ,

Pattie Maes on Ambient Intelligence

Posted about 1 year back at InfoQ Personalized Feed for unregistered user - Register to upgrade!

At OOPSLA 2007, Pattie Maes gave an interesting talk about the MIT ambient intelligence projects. One project, ReachMedia, was particularly interesting from an architectural, mashup and social networking perspective.

Some thoughts on Leopard

Posted about 1 year back at Luke Redpath - Home

Rather than spending time discussing Leopard’s much discussed new major features, I thought it would be interesting to point out some of the smaller, minor but useful updates that I have picked up on so far. I will update this list over the next week or two as I discover new things, good and bad:

  • Installation didn’t go too smoothly for me; there appears to be a bug in the install process affecting some machines that means that my hard drive wasn’t showing up in the “Select a destination” panel. Fortunately I was able to find out from the Apple discussion forums that after waiting 10 to 15 minutes your available drives will appear. One cup of tea later, and the install routine was back on track. I opted to perform an “Erase and Install” on my MacBook Pro to get rid of the sheer amount of crap that I had accumulated over the last 18 months which went as smoothly as I’d expected. I am yet to try the upgrade option on my Mac Mini so YMMV here.
  • My first impressions were that Leopard seemed faster and given that it was probably hard at work indexing my hard drive for Spotlight this was pretty impressive. My Airport problems in Tiger (the connection would frequently just stop responding or lose packets making it incredibly frustrating to do anything useful) appear to be resolved, at least, when running on mains power. When running on battery I still seem to be suffering from random disconnections although Leopard at least shows that it has lost its connection and reconnects pretty fast. This may be a problem with my router and still requires further investigation on my part.
  • The new dock is not as bad as everybody has made out and I have left it at the bottom for now even though I’m traditionally a dock-on-the-side person. The new Stacks functionality is useful and looks great although the cool “fan” effect only works when the dock is on the bottom. I imagine the novelty will wear off quite quickly and I will be back to having my dock on the side soon enough.
  • I’m sure that most Rails developers have been aware for a while now that Leopard will ship with Rails. The new Ruby/Rails stack in Leopard is well thought out and organised and comes with Ruby 1.8.6, Rails 1.2.3 and and a host of other useful gems. Rails itself is simply bundled as a gem and is easily updated. A good overview of the changes to Ruby in Leopard is available.
  • One utility that anybody who frequently uses SSH and public/private keys would not be without is SSHKeychain which integrates with Apple Keychain and acts as an SSH agent. In my experience SSHKeychain could be flaky and would sometimes silently crash for no apparent reason; good news then because as of Leopard, OSX maintains its own Keychain-integrated ssh-agent making passphrase-protected keys painless to use out of the box.
  • Speaking of which, the new Leopard Terminal finally supports tabs and is much easier to customize thanks to built-in themes. Despite some niggles (like not being able to jump to a specific tab using Cmd+number) I think that it is finally time to say goodbye to iTerm.
  • Connecting to your mail server over SSL using a self-signed certificate in Mail.app is no longer a pain in the arse. Before, Mail would prompt you that the server certificate was not from a trusted source every time you tried to connect unless you manually dragged the certificate to the desktop and added it to your keychain. In Leopard, Mail finally has an option to “always trust this certificate” as well as a number of other fine-grained trust options.
  • The new version of Front Row, based on the Apple TV software, seems like a regression to me. The new interface doesn’t seem as slick and suffers from some annoying bugs like not being able to view album artwork on a shared library and most annoying of all: there doesn’t seem to be any way to get Front Row to open on a second monitor. In Tiger, Front Row would always open on the primary display – this was documented behavior and in Leopard this behavior seems to have disappeared. I like to hook my MBP up to my plasma TV but now Front Row will only open on my MBP screen, even if its not the primary display, severely limiting its usefulness.
  • On the plus-side, Quicktime now has some useful full-screen options including options to stretch or fill a widescreen display when viewing videos in a 4:3 aspect ratio. Quicktime does work on a second display and allows you to select which screen it will display on in full-screen mode independently of your primary display settings.

Update 1: Airport Woes

When running in battery mode, Airport performance is horrendous, even after installing the latest keychain fix. The signal will go up and down and the transmit rate will jump up and down wildly between 0 and 54 before eventually just disconnecting – it seems to disconnect every couple of minutes.

The question is, is this a) a continuation of the connection stability problems I was having in Tiger but manifesting itself with different symptoms (or simply that Leopard is more responsive to Airport connection status changes than Tiger), b) an entirely new problem created by the latest Airport drivers, c) a hardware issue (dodgy Airport cards?) or d) a problem with my router.

Until I can test out Airport performance on my Mac Mini in Leopard, I can’t rule out D although the fact that many other people are having the same problem seems to suggests this is not the case. Airport performance seems reliable when connected to the mains, so maybe its an issue of power consumption by the Airport card?

Something else that I just noticed: there appears to be a rather nasty looking, if ultimately harmless, rendering bug when scrolling in Finder when in column mode:

More Airport updates

I’m beginning to become more and more convinced that the problem with Airport is a power issue. My Airport connection seems to be very stable with no disconnections when running on mains power but in battery mode the disconnections come very frequently. I’ve also managed to perform the following test with reliable results: plug in the power adapter and wait a few minutes to confirm a stable Airport connection. Now remove the power adapter – in my observations, about 80% of the time the Airport connection will drop within 10 seconds. This would indicate to me that the problem lies in the Airport card not being able to draw enough power to operate in a stable manner whilst running in battery mode.

Yet another update

I mentioned earlier that I was having trouble getting Front Row to display on my TV even though it was set as the primary monitor. Last night I gave it another try and et voila; Front Row was displaying on my TV. Further investigation led me to this post on the Apple discussion forums:

“Not sure if this is related to the bugs you are having, but one I’ve noticed and reported to apple is this: When you first start front row it appears on your primary display, it also remembers which display it first started on. So, if you exit but don’t kill the front row process (in activity manager), front row will always display on that initial primary monitor, even if you swap your primary/secondary in sys pref. I haven’t tried mirroring the displays. " cmendill, Apple Discussion Board

I haven’t had the change to verify this (yet) but it makes sense – if exiting Front Row doesn’t actually kill the process, then Front Row will not pick up changes to your primary display settings until you do. I still consider this to be a bug but at least it’s an explanation!

Leopard reviews:

Some thoughts on Leopard

Posted about 1 year back at Luke Redpath - Home

In case you missed the memo, Apple unleashed Leopard on Friday. I headed down to the Regent Street Apple Store to see if I could snag a copy; unfortunately, by 6pm the queue had reached epic proportions (at least a 45 minute wait) and I promptly left and instead chose to pick up a copy from Brent Cross on Saturday (I feel sorry for anybody who did bother queueing all evening – the Brent Cross store was no more busy than usual come Saturday afternoon).

Rather than spending time discussing Leopard’s much discussed new major features, I thought it would be interesting to point out some of the smaller, minor but useful updates that I have picked up on so far. I will update this list over the next week or two as I discover new things, good and bad:

  • Installation didn’t go too smoothly for me; there appears to be a bug in the install process affecting some machines that means that my hard drive wasn’t showing up in the “Select a destination� panel. Fortunately I was able to find out from the Apple discussion forums that after waiting 10 to 15 minutes your available drives will appear. One cup of tea later, and the install routine was back on track. I opted to perform an “Erase and Install� on my MacBook Pro to get rid of the sheer amount of crap that I had accumulated over the last 18 months which went as smoothly as I’d expected. I am yet to try the upgrade option on my Mac Mini so YMMV here.
  • My first impressions were that Leopard seemed faster and given that it was probably hard at work indexing my hard drive for Spotlight this was pretty impressive. My Airport problems in Tiger (the connection would frequently just stop responding or lose packets making it incredibly frustrating to do anything useful) appear to be resolved, at least, when running on mains power. When running on battery I still seem to be suffering from random disconnections although Leopard at least shows that it has lost its connection and reconnects pretty fast. This may be a problem with my router and still requires further investigation on my part.
  • The new dock is not as bad as everybody has made out and I have left it at the bottom for now even though I’m traditionally a dock-on-the-side person. The new Stacks functionality is useful and looks great although the cool “fan� effect only works when the dock is on the bottom. I imagine the novelty will wear off quite quickly and I will be back to having my dock on the side soon enough.
  • I’m sure that most Rails developers have been aware for a while now that Leopard will ship with Rails. The new Ruby/Rails stack in Leopard is well thought out and organised and comes with Ruby 1.8.6, Rails 1.2.3 and and a host of other useful gems. Rails itself is simply bundled as a gem and is easily updated. A good overview of the changes to Ruby in Leopard is available.
  • One utility that anybody who frequently uses SSH and public/private keys would not be without is SSHKeychain which integrates with Apple Keychain and acts as an SSH agent. In my experience SSHKeychain could be flaky and would sometimes silently crash for no apparent reason; good news then because as of Leopard, OSX maintains its own Keychain-integrated ssh-agent making passphrase-protected keys painless to use out of the box.
  • Speaking of which, the new Leopard Terminal finally supports tabs and is much easier to customize thanks to built-in themes. Despite some niggles (like not being able to jump to a specific tab using Cmd+number) I think that it is finally time to say goodbye to iTerm.
  • Connecting to your mail server over SSL using a self-signed certificate in Mail.app is no longer a pain in the arse. Before, Mail would prompt you that the server certificate was not from a trusted source every time you tried to connect unless you manually dragged the certificate to the desktop and added it to your keychain. In Leopard, Mail finally has an option to “always trust this certificate� as well as a number of other fine-grained trust options.
  • The new version of Front Row, based on the Apple TV software, seems like a regression to me. The new interface doesn’t seem as slick and suffers from some annoying bugs like not being able to view album artwork on a shared library and most annoying of all: there doesn’t seem to be any way to get Front Row to open on a second monitor. In Tiger, Front Row would always open on the primary display – this was documented behavior and in Leopard this behavior seems to have disappeared. I like to hook my MBP up to my plasma TV but now Front Row will only open on my MBP screen, even if its not the primary display, severely limiting its usefulness.
  • On the plus-side, Quicktime now has some useful full-screen options including options to stretch or fill a widescreen display when viewing videos in a 4:3 aspect ratio. Quicktime does work on a second display and allows you to select which screen it will display on in full-screen mode independently of your primary display settings.

Update 1: Airport Woes

When running in battery mode, Airport performance is horrendous, even after installing the latest keychain fix. The signal will go up and down and the transmit rate will jump up and down wildly between 0 and 54 before eventually just disconnecting – it seems to disconnect every couple of minutes.

The question is, is this a) a continuation of the connection stability problems I was having in Tiger but manifesting itself with different symptoms (or simply that Leopard is more responsive to Airport connection status changes than Tiger), b) an entirely new problem created by the latest Airport drivers, c) a hardware issue (dodgy Airport cards?) or d) a problem with my router.

Until I can test out Airport performance on my Mac Mini in Leopard, I can’t rule out D although the fact that many other people are having the same problem seems to suggests this is not the case. Airport performance seems reliable when connected to the mains, so maybe its an issue of power consumption by the Airport card?

Something else that I just noticed: there appears to be a rather nasty looking, if ultimately harmless, rendering bug when scrolling in Finder when in column mode:

More Airport updates

I’m beginning to become more and more convinced that the problem with Airport is a power issue. My Airport connection seems to be very stable with no disconnections when running on mains power but in battery mode the disconnections come very frequently. I’ve also managed to perform the following test with reliable results: plug in the power adapter and wait a few minutes to confirm a stable Airport connection. Now remove the power adapter – in my observations, about 80% of the time the Airport connection will drop within 10 seconds. This would indicate to me that the problem lies in the Airport card not being able to draw enough power to operate in a stable manner whilst running in battery mode.

Yet another update

I mentioned earlier that I was having trouble getting Front Row to display on my TV even though it was set as the primary monitor. Last night I gave it another try and et voila; Front Row was displaying on my TV. Further investigation led me to this post on the Apple discussion forums:

“Not sure if this is related to the bugs you are having, but one I’ve noticed and reported to apple is this: When you first start front row it appears on your primary display, it also remembers which display it first started on. So, if you exit but don’t kill the front row process (in activity manager), front row will always display on that initial primary monitor, even if you swap your primary/secondary in sys pref. I haven’t tried mirroring the displays. � cmendill, Apple Discussion Board

I haven’t had the change to verify this (yet) but it makes sense – if exiting Front Row doesn’t actually kill the process, then Front Row will not pick up changes to your primary display settings until you do. I still consider this to be a bug but at least it’s an explanation!

Leopard reviews:

Some thoughts on Leopard

Posted about 1 year back at Luke Redpath - Home

In case you missed the memo, Apple unleashed Leopard on Friday. I headed down to the Regent Street Apple Store to see if I could snag a copy; unfortunately, by 6pm the queue had reached epic proportions (at least a 45 minute wait) and I promptly left and instead chose to pick up a copy from Brent Cross on Saturday (I feel sorry for anybody who did bother queueing all evening – the Brent Cross store was no more busy than usual come Saturday afternoon).

Rather than spending time discussing Leopard’s much discussed new major features, I thought it would be interesting to point out some of the smaller, minor but useful updates that I have picked up on so far. I will update this list over the next week or two as I discover new things, good and bad:

  • Installation didn’t go too smoothly for me; there appears to be a bug in the install process affecting some machines that means that my hard drive wasn’t showing up in the “Select a destination” panel. Fortunately I was able to find out from the Apple discussion forums that after waiting 10 to 15 minutes your available drives will appear. One cup of tea later, and the install routine was back on track. I opted to perform an “Erase and Install” on my MacBook Pro to get rid of the sheer amount of crap that I had accumulated over the last 18 months which went as smoothly as I’d expected. I am yet to try the upgrade option on my Mac Mini so YMMV here.
  • My first impressions were that Leopard seemed faster and given that it was probably hard at work indexing my hard drive for Spotlight this was pretty impressive. My Airport problems in Tiger (the connection would frequently just stop responding or lose packets making it incredibly frustrating to do anything useful) appear to be resolved, at least, when running on mains power. When running on battery I still seem to be suffering from random disconnections although Leopard at least shows that it has lost its connection and reconnects pretty fast. This may be a problem with my router and still requires further investigation on my part.
  • The new dock is not as bad as everybody has made out and I have left it at the bottom for now even though I’m traditionally a dock-on-the-side person. The new Stacks functionality is useful and looks great although the cool “fan” effect only works when the dock is on the bottom. I imagine the novelty will wear off quite quickly and I will be back to having my dock on the side soon enough.
  • I’m sure that most Rails developers have been aware for a while now that Leopard will ship with Rails. The new Ruby/Rails stack in Leopard is well thought out and organised and comes with Ruby 1.8.6, Rails 1.2.3 and and a host of other useful gems. Rails itself is simply bundled as a gem and is easily updated. A good overview of the changes to Ruby in Leopard is available.
  • One utility that anybody who frequently uses SSH and public/private keys would not be without is SSHKeychain which integrates with Apple Keychain and acts as an SSH agent. In my experience SSHKeychain could be flaky and would sometimes silently crash for no apparent reason; good news then because as of Leopard, OSX maintains its own Keychain-integrated ssh-agent making passphrase-protected keys painless to use out of the box.
  • Speaking of which, the new Leopard Terminal finally supports tabs and is much easier to customize thanks to built-in themes. Despite some niggles (like not being able to jump to a specific tab using Cmd+number) I think that it is finally time to say goodbye to iTerm.
  • Connecting to your mail server over SSL using a self-signed certificate in Mail.app is no longer a pain in the arse. Before, Mail would prompt you that the server certificate was not from a trusted source every time you tried to connect unless you manually dragged the certificate to the desktop and added it to your keychain. In Leopard, Mail finally has an option to “always trust this certificate” as well as a number of other fine-grained trust options.
  • The new version of Front Row, based on the Apple TV software, seems like a regression to me. The new interface doesn’t seem as slick and suffers from some annoying bugs like not being able to view album artwork on a shared library and most annoying of all: there doesn’t seem to be any way to get Front Row to open on a second monitor. In Tiger, Front Row would always open on the primary display – this was documented behavior and in Leopard this behavior seems to have disappeared. I like to hook my MBP up to my plasma TV but now Front Row will only open on my MBP screen, even if its not the primary display, severely limiting its usefulness.
  • On the plus-side, Quicktime now has some useful full-screen options including options to stretch or fill a widescreen display when viewing videos in a 4:3 aspect ratio. Quicktime does work on a second display and allows you to select which screen it will display on in full-screen mode independently of your primary display settings.

Leopard reviews:

Some thoughts on Leopard

Posted about 1 year back at Luke Redpath - Home

In case you missed the memo, Apple unleashed Leopard on Friday. I headed down to the Regent Street Apple Store to see if I could snag a copy; unfortunately, by 6pm the queue had reached epic proportions (at least a 45 minute wait) and I promptly left and instead chose to pick up a copy from Brent Cross on Saturday (I feel sorry for anybody who did bother queueing all evening – the Brent Cross store was no more busy than usual come Saturday afternoon).

Rather than spending time discussing Leopard’s much discussed new major features, I thought it would be interesting to point out some of the smaller, minor but useful updates that I have picked up on so far. I will update this list over the next week or two as I discover new things, good and bad:

  • Installation didn’t go too smoothly for me; there appears to be a bug in the install process affecting some machines that means that my hard drive wasn’t showing up in the “Select a destination” panel. Fortunately I was able to find out from the Apple discussion forums that after waiting 10 to 15 minutes your available drives will appear. One cup of tea later, and the install routine was back on track. I opted to perform an “Erase and Install” on my MacBook Pro to get rid of the sheer amount of crap that I had accumulated over the last 18 months which went as smoothly as I’d expected. I am yet to try the upgrade option on my Mac Mini so YMMV here.
  • My first impressions were that Leopard seemed faster and given that it was probably hard at work indexing my hard drive for Spotlight this was pretty impressive. My Airport problems in Tiger (the connection would frequently just stop responding or lose packets making it incredibly frustrating to do anything useful) appear to be resolved, at least, when running on mains power. When running on battery I still seem to be suffering from random disconnections although Leopard at least shows that it has lost its connection and reconnects pretty fast. This may be a problem with my router and still requires further investigation on my part.
  • The new dock is not as bad as everybody has made out and I have left it at the bottom for now even though I’m traditionally a dock-on-the-side person. The new Stacks functionality is useful and looks great although the cool “fan” effect only works when the dock is on the bottom. I imagine the novelty will wear off quite quickly and I will be back to having my dock on the side soon enough.
  • I’m sure that most Rails developers have been aware for a while now that Leopard will ship with Rails. The new Ruby/Rails stack in Leopard is well thought out and organised and comes with Ruby 1.8.6, Rails 1.2.3 and and a host of other useful gems. Rails itself is simply bundled as a gem and is easily updated. A good overview of the changes to Ruby in Leopard is available.
  • One utility that anybody who frequently uses SSH and public/private keys would not be without is SSHKeychain which integrates with Apple Keychain and acts as an SSH agent. In my experience SSHKeychain could be flaky and would sometimes silently crash for no apparent reason; good news then because as of Leopard, OSX maintains its own Keychain-integrated ssh-agent making passphrase-protected keys painless to use out of the box.
  • Speaking of which, the new Leopard Terminal finally supports tabs and is much easier to customize thanks to built-in themes. Despite some niggles (like not being able to jump to a specific tab using Cmd+number) I think that it is finally time to say goodbye to iTerm.
  • Connecting to your mail server over SSL using a self-signed certificate in Mail.app is no longer a pain in the arse. Before, Mail would prompt you that the server certificate was not from a trusted source every time you tried to connect unless you manually dragged the certificate to the desktop and added it to your keychain. In Leopard, Mail finally has an option to “always trust this certificate” as well as a number of other fine-grained trust options.
  • The new version of Front Row, based on the Apple TV software, seems like a regression to me. The new interface doesn’t seem as slick and suffers from some annoying bugs like not being able to view album artwork on a shared library and most annoying of all: there doesn’t seem to be any way to get Front Row to open on a second monitor. In Tiger, Front Row would always open on the primary display – this was documented behavior and in Leopard this behavior seems to have disappeared. I like to hook my MBP up to my plasma TV but now Front Row will only open on my MBP screen, even if its not the primary display, severely limiting its usefulness.
  • On the plus-side, Quicktime now has some useful full-screen options including options to stretch or fill a widescreen display when viewing videos in a 4:3 aspect ratio. Quicktime does work on a second display and allows you to select which screen it will display on in full-screen mode independently of your primary display settings.

Update 1: Airport Woes

When running in battery mode, Airport performance is horrendous, even after installing the latest keychain fix. The signal will go up and down and the transmit rate will jump up and down wildly between 0 and 54 before eventually just disconnecting – it seems to disconnect every couple of minutes.

The question is, is this a) a continuation of the connection stability problems I was having in Tiger but manifesting itself with different symptoms (or simply that Leopard is more responsive to Airport connection status changes than Tiger), b) an entirely new problem created by the latest Airport drivers, c) a hardware issue (dodgy Airport cards?) or d) a problem with my router.

Until I can test out Airport performance on my Mac Mini in Leopard, I can’t rule out D although the fact that many other people are having the same problem seems to suggests this is not the case. Airport performance seems reliable when connected to the mains, so maybe its an issue of power consumption by the Airport card?

Something else that I just noticed: there appears to be a rather nasty looking, if ultimately harmless, rendering bug when scrolling in Finder when in column mode:

More Airport updates

I’m beginning to become more and more convinced that the problem with Airport is a power issue. My Airport connection seems to be very stable with no disconnections when running on mains power but in battery mode the disconnections come very frequently. I’ve also managed to perform the following test with reliable results: plug in the power adapter and wait a few minutes to confirm a stable Airport connection. Now remove the power adapter – in my observations, about 80% of the time the Airport connection will drop within 10 seconds. This would indicate to me that the problem lies in the Airport card not being able to draw enough power to operate in a stable manner whilst running in battery mode.

Yet another update

I mentioned earlier that I was having trouble getting Front Row to display on my TV even though it was set as the primary monitor. Last night I gave it another try and et voila; Front Row was displaying on my TV. Further investigation led me to this post on the Apple discussion forums:

“Not sure if this is related to the bugs you are having, but one I’ve noticed and reported to apple is this: When you first start front row it appears on your primary display, it also remembers which display it first started on. So, if you exit but don’t kill the front row process (in activity manager), front row will always display on that initial primary monitor, even if you swap your primary/secondary in sys pref. I haven’t tried mirroring the displays. ” cmendill, Apple Discussion Board

I haven’t had the change to verify this (yet) but it makes sense – if exiting Front Row doesn’t actually kill the process, then Front Row will not pick up changes to your primary display settings until you do. I still consider this to be a bug but at least it’s an explanation!

Leopard reviews:

Episode 77: Destroy Without JavaScript

Posted about 1 year back at Railscasts

If the user has JavaScript disabled, the "Destroy" link might not work properly. In this episode I will explore a number of ways to work around this issue.

Episode 77: Destroy Without JavaScript

Posted about 1 year back at Railscasts

If the user has JavaScript disabled, the "Destroy" link might not work properly. In this episode I will explore a number of ways to work around this issue.

Mozilla Labs’ announces Prism

Posted about 1 year back at InfoQ Personalized Feed for unregistered user - Register to upgrade!

On Friday, Mozilla Labs’ announced Prism, their entry into the budding market-trend of platforms for running web applications on the desktop, similar to Adobe AIR.

"Introduction to Ruby" Video Series

Posted about 1 year back at Ruby Inside

Classxjxjs

Introduction to Ruby is a well produced set of four videos by Lucas Holland that provide an introduction to both Ruby and object oriented programming itself. The first video looks at what Ruby actually is and how it handles OOP concepts. The second video covers Ruby's installation. The third looks at some of the tools and programs that come with a regular Ruby installation, and the fourth looks more into actually writing some code and processing data.

Bundle-Fu: Rails plugin to reduce overhead by bundling assets together

Posted about 1 year back at Ruby Inside

Bundle-Fu
Bundle-Fu is a new plugin by Tim Harper that can bundle CSS and JavaScript content from separate files into one file each automatically to reduce the amount of <scrip> and <link> includes required on the pages within your Rails applications.

Book Review: Pro Active Record

Posted about 1 year back at has_many :through - home

I've been wanting to start doing some book reviews for a while, so here goes. The folks at Apress have been kind enough to send me review copies of a couple books, so I'm going to start with one of them. First up, Pro Active Record.

Title: Pro Active Record: Databases with Ruby and Rails
Authors: Kevin Marshall, Chad Pytel, Jon Yurek
Publisher: Apress

My first thought at seeing this book was, "Hey, a book all about ActiveRecord! Cool!" It's nice to finally see a volume dedicated to ActiveRecord, since it's my favorite part of Rails. However, this book ends up being something of a mixed bag. There's a lot of good information in it, and it does a decent job of covering all the basics. There are even some excellent parts here and there. But the book also has some problems, so it's hard for me to give it an unequivocal recommendation. Nevertheless, it does have value and fills a needed spot, so it may be what you're looking for.

The Good

Let's start by taking a look at what the book provides. As I said, it covers all the basics.

Pro Active Record starts with a good introduction to ActiveRecord, going over a bit of the history, how to install it, and shows a small example program. The best part of this chapter is the section that shows all the configuration settings for the different database adapters. It's nice to see all that in one place.

Chapter 2 digs into the foundation of ActiveRecord and how it maps objects to SQL. It covers CRUD operations, transactions, and locking. I like how it is full of examples that show an AR API call and the resulting SQL.

Chapter 3 is entitled Setting Up Your Database, but it's mainly about using migrations. It does however start with a good summary of the main ActiveRecord conventions for table naming and structure. It would be even better to see a summary of all the magic fields in one place (e.g. features like counter cache and optimistic locking).

Chapter 4 is where things start to get interesting. This chapter gets into the meat and potatoes (or granola and tofu for us Californians) of ActiveRecord. The three main topics are Callbacks (what I call the life-cycle features), Associations, and Validations. This is where you start to get a feel for what using ActiveRecord is like. Every major feature comes with some code examples, which is a good thing, though as contrived examples go some of them are pretty uninspired. Probably the biggest weakness in terms of content is that the section on associations doesn't show more than the most basic use cases. ActiveRecord associations are a rich topic and could easily cover a whole chapter, if not several. But for a beginner's book on ActiveRecord, this is probably enough. Though not seeing any mention of association extensions made me sad.

Chapter 5 is like the whole book in microcosm. Some good stuff, some stupid stuff, but not a waste of time. It covers some "bonus features" of ActiveRecord. The section on observers was an interesting start, but the topic deserves more than just one page. On the other hand, there are thirteen whole pages wasted covering the List, Tree and Nested Set features that everyone has known for ages were slated to be pulled from ActiveRecord (and were just extracted and turned into plugins). The section on aggregations is adequate, but the examples obfuscate more than they reveal. And yet, the chapter recovers and even shines with one of the best discussions of metaprogramming in Ruby and ActiveRecord that I've yet to read. Really, I'd almost say it's worth it to get this book just for the Extending Active Record section of this chapter. The thing that makes it work is how they show a problem, then an evolving series of solutions that do similar things but with more sophistication as they progress. At each step there is a discussion of the advantages and drawbacks of each approach. If the whole book was up to this standard, it would be a no-brainer to recommend it to everyone.

Chapter 6 introduces ActiveRecord testing and debugging. It includes a rather light discussion of writing tests and the basics of using fixtures. Most of the testing section covers the assertion methods in Test::Unit. While this book's focus isn't testing, it's a shame that there weren't at least a few good examples or some deeper how-to discussion. The world really needs a good book on testing Rails (so get off your butts, guys - you know who you are!). There follows a section that lists and describes the ActiveRecord exception classes. Finally there's a section on debugging tips, which is mainly about using logging. I was disappointed the chapter didn't include any mention of using irb (or the Rails console) for experimenting with code or debugging, and that there was no mention of using the Ruby debugger.

Chapter 7 addresses how to work with legacy databases. This is one of the trickiest things to do with ActiveRecord, because you don't get to go with the flow and have to do a lot more configuration than normal. The first part of the chapter describes the various configuration methods ActiveRecord provides to override its default conventions, and it's great to see them listed in one place like that. Then there is a slow dive into the bowels of ActiveRecord, covering find_by_sql and some of the low-level database APIs. There's also a section on import/export of your favorite interchange formats (xml, yaml, csv). What is missing is any discussion of how to map typical un-Rails-ish database usage patterns into ActiveRecord. For example, how to deal with non-integer primary keys, composite keys, foreign key constraints, enums, etc. That would have made this chapter a real winner.

Chapter 8 is the catch-all for everything that didn't fit in the other chapters. It has a section on reading the ActiveRecord source code that provides a roadmap for finding your way around. There's a decent discussion of various alternatives to ActiveRecord (other Ruby ORM packages), though it misses some recent additions to the field like Sequel and DataMapper. The FAQ section is pretty good, and provided a number of answers that were missing from previous chapters. One answer even includes some code from my own blog (thanks for giving the proper attribution!).

Finally, there is an Appendix that documents the core ActiveRecord API.

Looking back, that's a pretty good chunk of information on ActiveRecord, and probably enough to get anyone started. It's also very readable (though all the anecdotes about Kevin got to be a bit much). I thought the organization was good and followed a logical progression.

The Bad (and The Ugly, because you expected it)

Unfortunately, for all the book provides, there are enough problems with it that I have to wonder if it missed out on some much-needed editing.

To begin, this book doesn't say anywhere what version of ActiveRecord (or Rails) it is documenting. It makes me wonder if the authors were all on the same page here, because while most places seem to be working with Rails 1.2, some look like they are talking about Rails 2.0 code. Since Rails 2.0 is being released any day now, that's an important issue. There have been a lot of changes to ActiveRecord since Rails 1.2, and readers may be pretty confused about the applicability of the information. If nothing else, this book needs an update to get it in sync with Rails 2.0.

Next off, the book doesn't come with any downloadable example code. I've reached the point where I think that is a basic requirement for a software book. I think it's important to provide code that you can play with, or can use to copy/paste into your own code. It's also possible to provide a lot more code in the example projects, since putting all that code in the book is usually too much. And maybe most of all, having real code from which you grab excerpts for the book means that you can have confidence the code actually works. The code examples in Pro Active Record were obviously written in a word processor and never executed, since many of them wouldn't work correctly or even compile. I've messed up doing that in my own blog postings enough that I've learned that lesson.

One of the problems that affects the code samples disproportionately is that the book is riddled with typos and capitalization errors. It looks like Word's auto-capitalization messed a lot of stuff up badly, especially the example code. Unfortunately, when you're showing source code for a language that is case-sensitive, what might otherwise be a minor annoyance can become a significant issue. This is the sort of thing that is so easily prevented that I don't understand how it can happen.

More seriously though, there are some glaring factual errors. For example, Chapter 2 states that ActiveRecord#update_attributes doesn't run validations on the model. But it's a totally common use case to use update_attributes like so:

def update
  @user = User.find(params[:id])
  if @user.update_attributes(params[:user])
    redirect_to user_path(@user)
  else
    render :action => "edit" 
  end
end

That's even how it's done in the standard scaffolding now. If update_attributes didn't run validations, that code wouldn't work. Now, the update_attribute (note the singular) method does not run validations, but it is used quite differently from the plural form method. I could see how a casual reader of the ActiveRecord API might get confused, but not someone who had ever written a typical CRUD Rails application. Oddly enough, they get it right in the API summary appendix. Ah, the joys of collaborative authorship.

The book also states that there is a standard db:bootstrap rake task in Rails, which doesn't exist even in Rails 2.0 (though Rick Olson and others have rolled their own). It says you can explicitly delete object instances in Ruby, something that is impossible in a garbage collected language like Ruby. And it says the :id attribute is optional in has_and_belongs_to_many, where it will in reality break certain things if you include it. They also claim that it doesn't matter which ActiveRecord class you call find_by_sql on, when in actuality the class determines what kind of model object is returned from the call. Those are just some examples I caught; there are plenty of others.

This may all sound nit-picky, but I think if you can't have confidence in the accuracy of a reference book, then you can't truly rely on any of the information in it. Fortunately for the authors, none of these detail errors are major problems and they could be corrected with a good technical edit of the book. Perhaps they can be corrected in the ebook without waiting for a second printing

Bottom Line

If you're looking to get started with ActiveRecord, Pro Active Record is a pretty good place to start. It has everything you need to get your bearings and start your journey. Be advised, however, that it's not going to take you much past the start of that journey. And at least with the current edition, you'll need to double check the directions along the way.

Reviewing the Review

Since this was my first book review here, I'd like to get some feedback. What did you all think of it? Useful? Too critical? Too short? Too long? Not enough pie?

I'm intending to do more of these (maybe one every month or two), so tell me what works and what doesn't.

Jay Fields and Zak Tamsen on Domain Specific Languages

Posted about 1 year back at InfoQ Personalized Feed for unregistered user - Register to upgrade!

Jay Fields and Zak Tamsen talked with InfoQ about Domain Specific Languages (DSLs), and how they have successfully used them in their projects at ThoughtWorks to empower businesses, reduce development time, and increase the agility of projects.

Trying Out wmii

Posted about 1 year back at Blog Posts : Nex3

Wow, I’ve been busy. I just had my math midterm, not to mention the first rehearsal for the play I’m directing.

I did manage to find time on Friday to go on a little adventure, though. Ever since I moved to Linux, I’ve been finding more and more that I like a certain kind of tool. Emacs, for example. It may be complicated and have a steep learning curve, but the power it offers is almost unparalleled.

LaTeX, too. Sure it was kind of a pain to learn, but my math is so beautiful. And it’s so easy to write, now that I (mostly) know how.

I’ll save the rest of my lecture on UI for another post, but suffice it to say that I seem to like tools that have a steep learning curve but are very worth it in terms of power and productivity. Tools that aren’t built for everyone.

I was thinking about this last Friday. And I was thinking about window managers.

I had been using Compiz Fusion for a while, but my computer kept crashing. So I switched back to Metacity, the default, if boring window manager for Gnome.

As I was eliciting pity from a friend who had Compiz working flawlessly, it hit me. Now that I was no longer bound by the golden chain of eye candy, I had a great opportunity to try out a new, interesting window manager.

wmii

I decided to give wmii a go. This was mostly because it was the first thing that came to mind when I thought “interesting window manager.” I think _why uses it, and he seems to be a pretty good judge of what’s interesting.

So what makes for an “interesting” window manager? All I was really looking for was something like what I’d found in Emacs and LaTeX. I wanted a window manager that was powerful, extensible, and usable in that certain hard-to-define seemingly-unusable way.

I also wanted something distinctly different than the previous window managers I’d used. Metacity, Compiz, even whatever the Windows and OSX window managers are called - they’re all based on the same paradigm of “floating boxes that you move and squish.” I wanted something with a totally different paradigm.

wmii certainly fit that bill. It has several interesting features that seem terribly revolutionary to me. Now, I know there are plenty of other window managers out there with similar featuresets. Much of what I find amazing about wmii isn’t actually innovation on the part of the devs (although some of it certainly is). But for me, coming from the insular world of “normal” managers, it’s all mind-blowing.

Dynamic Window Management

One thing wmii shares with some other window managers (although none I’ve used) is that it’s “dynamic.” This means that it actually manages your windows. With Metacity, you have to move all the windows around on your own, resize them, place them just so. wmii does that for you.

When you first start wmii, it just gives you an empty screen with a status bar. As you open applications, it evenly distributes them so that they’re all as wide as the screen and their combined height fills up the entire screen.

You can then pull applications into new columns, so that you might have a stack of three terminals on one half of the screen and one tall Emacs instance on the other. wmii makes sure it’s all organized and sized properly. All you have to do is guide it.

Tagging

A second incredibly cool thing about wmii is that it allows you to tag windows. This is in lieu of a conception of workspaces or minimized applications that other window managers have.

The basic idea is incredibly simple. You can give each window one or more alphanumeric tags. At any given time, you’re displaying all (and only) the windows with a given tag.

Incredibly simple. But also incredibly powerful.

For instance, I like to use tags to divide up my tasks. As I type this, I’m viewing my “blog” tag. I have two apps tagged as “blog:” Emacs for creating the thing, and Firefox for looking up various minutiae and links. Only those two apps are visible right now.

I also have a “hw” tag for doing my math homework. I have Emacs tagged as “hw,” for writing LaTeX, and two Evince windows, one for viewing my compiled LaTeX, and one for viewing the assignment.

Then I have a “prog” tag for programming. This again has Emacs, as well as two terminals. I also have various other tags for various things.

Note that these are all actually the same Emacs instance. It just has three tags, so I can view it in three places. I can even organize it in different ways in the various different views. It could be next to the terminals in “prog” but above Firefox in “blog.”

Scripting

The third really cool thing about wmii is its scripting interface. It’s in the style of Plan 9, a style which I had not previously encountered. It’s based on a concept of files being a metaphor for everything, which is rooted in Unixy stuff like /dev/stdin and sockets and suchlike.

So for wmii, everything is a file or folder. The virtual filesystem is accessed via the wmiir command, using wmiir read <filename> or wmiir write <filename>, which operate on standard input and output, respectively.

For example, the folder representing the selected window is /client/sel. To set the tags on that window to “foo” and “bar”, all you need to do is run

echo "foo+bar" | wmiir write /client/sel/tags

Everything about wmii is accessible through this virtual filesystem. Beats DBus by a longshot.

Installation

Installing wmiir is quite simple. It’s packaged by Ubuntu, so all I had to do was run sudo apt-get install wmii. It has a very simple build process, too, though, so just downloading the source and running make and sudo make install should work for non-Ubuntu folks.

Actually getting it to run is a bit more difficult. You actually have to (gasp!) edit a file. Just open up ~/.xsession and add exec wmii to the top.

If you don’t have an xsession file, just create a new one. Be sure to add an sh-shebang, #!/bin/sh, at the top. I’m not sure this is actually important, but better safe than sorry.

Once that’s set up, all you have to do is restart. wmii should start instead of, say, Metacity.

Now, the first thing I noticed when I started up wmii was that all my Gnome settings – most noticeably, my window theme – were gone. Luckily, this was easy to fix. Just run gnome-settings-manager and all should be well. You can even add gnome-settings-manager & to your .xsession to have it run automatically.

I also wanted to talk about my personal configuration of wmii, but it’s late, I’m tired, and when I’m tired I don’t write well. So I’ll call it quits for now and continue this some other time.

Breakage in Leopard - RMagick fails to find libdpstk.1.dylib

Posted about 1 year back at Pennysmalls

Some small breakage in Leopard - it seems a couple of parts of X11 have been removed. Starting my rails setup, which has GraphicsMagick installed (read here for how), results in this:

dyld: NSLinkModule() error
dyld: Library not loaded: /usr/X11R6/lib/libdpstk.1.dylib
  Referenced from: /usr/local/lib/ruby/gems/1.8/gems/rmagick-1.15.9/lib/RMagick.bundle
  Reason: image not found
Trace/BPT trap

Those dylib files do not exist any more. But… I have a backup (I hope you do). I ended up doing this:

$ cd /Volumes/BackupDisk/usr/X11R6/lib
$ sudo cp libdps*1.0* /usr/X11R6/lib/
$ sudo ln -s /usr/X11R6/lib/libdpstk.1.0.dylib /usr/X11R6/lib/libdpstk.1.dylib
$ sudo ln -s /usr/X11R6/lib/libdps.1.0.dylib /usr/X11R6/lib/libdps.1.dylib

All fixed.

Without a backup you are going to need to find those two files from somewhere. I’m not sure I can legally post them here, but email me if you are stuck.


1 ... 476 477 478 479 480 ... 634