Git Bisect For Great Justice

About a week ago, we had an elusive error that appeared when we deployed our latest app to staging. Suddenly, any submission resulted in a “Stack trace too deep” error that gave no meaningful way to determine where the issue was coming from. We were stuck for a couple of days, but then I was reminded of git bisect. git bisect is a great way to trace down problems to a specific commit for purposes of isolation or blame (though I’d suggest against the latter). Like you’d expect from a programmer, it even searches efficiently, using a binary search, hence bisect. ...

April 12, 2012 · 2 min · Mark Simoneau

CanCan and Devise with nil current_user on POST

I had a Rails 2.3.X app that I migrated to Rails 3. When I did, I upgraded CanCan and Devise. Suddenly, my delete links weren’t working. I remembered that unobtrusive JS was the default so I hopped into the application.html.erb template and added the relevant javascript tags. ... <head> ... <%= javascript_include_tag 'prototype', 'rails', 'application' %> ... </head> ... Great! Now my delete links weren’t just links to the show action, but when I did click them I noticed that a CanCan error appeared. After some debug statements, it became clear that current_user wasn’t set, but only on the POST/DELETE methods. Hmmm. What could be causing this? ...

April 11, 2012 · 1 min · Mark Simoneau

Best Way to Bundle

Thanks to the RubyRogues Episode on Bundler, I learned the best way to use bundler. You want to always use the “pessemistic” version numbers for a handful of reasons. gem 'rails', '~> 3.0.3' gem 'rspec', '~> 2.7.0' This allows the most efficient resolution of gem dependencies (in this case, any version of rails from 3.0.3 up to and not including 3.1, and any version of rspec from 2.7.0 up to and not including 2.8) and the added benefit of allowing you to easily patch everything safely using only bundle update. ...

March 14, 2012 · 1 min · Mark Simoneau

Don't Use DateTime in Rails 3

Rails 3 has good TimeZone support built in, but you have to use the right Date and Time classes to get full support. If you have this set in your application.rb config.active_record.default_timezone = :local, then you really need to use Time so that it properly identifies itself as being in the local timezone and not in UTC when passing to the database insert. ree-1.8.7-2011.03 :001 > DateTime.parse('2011-11-27 12:00:00 +0000') => Sun, 27 Nov 2011 12:00:00 +0000 ree-1.8.7-2011.03 :002 > DateTime.parse('2011-11-27 12:00:00') => Sun, 27 Nov 2011 12:00:00 +0000 ree-1.8.7-2011.03 :003 > Time.parse('2011-11-27 12:00:00 +0000') => Sun Nov 27 06:00:00 -0600 2011 ree-1.8.7-2011.03 :004 > Time.parse('2011-11-27 12:00:00') => Sun Nov 27 12:00:00 -0600 2011 So basically, Time.parse always returns the value in the local timezone, DateTime.parse always returns the value in UTC. To get complete compatibility, always use Time.parse. Trust me, I learned the hard way :)

November 29, 2011 · 1 min · Mark Simoneau

Code Kata #1 - Supermarket Pricing

Taken from Code Kata’s by Dave Thomas The basic premise of this Kata is asking how you would model super market pricing. What is the price of an item? What is the cost? How do you handle price-per-pound? Buy 2 get one free? What’s the value of stock? It’s interesting, and I thought I’d take the time to blog through my thought process. I’d like to iterate over the design and see where it takes me. First task is to represent a product. ...

October 14, 2011 · 4 min · Mark Simoneau

Migrating Serialized Columns in Rails

I recently switched a serialized column in Rails from one type (Hash) to another (OpenStruct) and ran into a little problem when I tried to migrate, namely, that loading the model threw a SerializationTypeMismatch error. Hmm… how am I going to get at the base YAML and translate all of these without being brittle? The answer is to copy the column, nullify it, and the load the raw YAML manually: ...

March 3, 2011 · 1 min · Mark Simoneau

A Ruby Scoping Gotcha?

Let’s take this basic class: class TestClass attr_accessor :one def my_method(branch=true) if branch puts "Do nothing to modify `one`" else puts "Modify `one` but it's a local variable" one = "test" end one # local variable end def my_non_modifying_method(branch=true) if branch puts "Do nothing to modify `one`" else puts "Do nothing to modify `one` either" end one #method call end end o = TestClass.new o.one = "Value" puts o.my_method => nil #might expect 'Value' if you're not paying attention puts o.my_non_modifying_method #expects "Value" => "Value" puts o.my_method(false) => "test" puts o.my_non_modifying_method(false) #expects "Value" => "Value" So remember, if you create any local variables anywhere in your method, even if they’re not called, they override the accessor methods and will give you results you’re not expecting. To get around it, make sure you always use self.accessor= to assign values when there is ambiguity.

August 27, 2010 · 1 min · Mark Simoneau

Optional Heirarchal Checkbox Selection with Nested Attributes in Rails

I had a process where I wanted users to fill out a survey which had hierarchal categories AND be able to specify some additional data for specific capabilities that the user had. Now, you could easily do this for a small subset and hand-code every item, but I wanted a flexible survey system that allowed true hierarchy and generalized code. Let’s start off with the basic survey and capabilities models and relationships: ...

June 4, 2010 · 4 min · Mark Simoneau

Rails Controller Specs with users, roles and nested routes

I’ve long put off testing my controllers because of user authentication and nested controllers, dealing with stubs, etc. But today, a fully working test! As background, Advertisers have many trackers and the routes look like this: ActionController::Routing::Routes.draw do |map| map.resources :advertisers do |advertisers| advertisers.resources :trackers end end To set everything up in the specs, I included all the files in the spec/support directory and used Mocha as my mock framework Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f} Spec::Runner.configure do |config| config.mock_with :mocha end Then I set up my factories (rather than fixtures) using Factory Girl ...

March 3, 2010 · 3 min · Mark Simoneau

PDF Generation in Rails... The Right Way

As long as we’re talking about efficiency here, one of the ways to be more efficient is to use the right tool for the job. I’ve done PDF generation on 3 different projects but the PDF generation I did yesterday was by far the easiest. What I thought would take me 2 days ended up taking about 3 hours (with research, etc). If you’re not using Ruby to automate some part of your job or life, I feel sad for you (at least a little). The next time you need to generate PDFs, why not try out the excellent Prawn library? Not familiar, you say? Well, let’s dive right in, shall we? ...

February 12, 2010 · 3 min · Mark Simoneau