Dynamically add class methods in Rails

I have a Product class which has a small number of records in the db, each of which has a name, eg “millionaire”.  Rather than keep typing Product.find_by_name(“millionaire”) i want to type Product.millionaire.

I could use method_missing for this but that means every time Product gets a method it doesn’t recognise it loads all the products out of the db – not very good.  Instead, let’s use define_method.  Normally define_method adds instance methods to a class but we can add them at the class level like so.  In the product class:

class << self
  Product.find(:all, :select => "name").collect(&:name).each do |name|
    define_method name.to_sym do

Now, when the classes are loaded, which happens only at restart in production mode, the methods are created.  No method_missing required and no db access besides the single access at class load.

Gem magic – copying gems from a server to your local repository

I recently upgraded to Ubuntu 9.10, wiping everything, formatting my partition and starting again.  There was the usual sunday full of software installing hassles.  But, i discovered one thing that made my life a ton easier, thanks to Marnen Laibow-Koser on http://ruby-forum.com

My situation (a common one) was that i had several apps deployed on servers, and i wanted my development environment to match that of the servers as closely as possible.  This means having the same gems as the servers, and, crucially, having the same versions of those gems.  Normally this would be a big pain in the ass, hunting around for places to get old out of date gems from.  Not so, however – i ssh onto my server and run

gem server & #the & is just to make it run in the background

This starts a gem server running on port 8808.

Now, back in my local machine, i can type

sudo gem install insert-gem-name-here –remote –source http://mywebsite.com:8808

and, hey presto, i get the version that’s on the server, installed locally.  Even better, you can pass it a list of all the gems you need at once, and go and drink a beer to celebrate the utter wonderfulness of rubygems.  Repeat for every app you’re working on, pointing it at it’s relevant server.  Huzzah!

Thanks Marnen!

BASH howto – find files with required text in an svn managed folder

This is just a bash (linux/mac terminal) one-liner to look for files with matching text in my rails app, which i do all the time.  It’s a lot faster and more useful than Nautilus’ file search, and is useful when you don’t use any IDEs.  Personally i do everything in the command line and gedit, nice and simple.

Here i’m looking for the string “ratings” in my views folder and my public/javascripts folder

This is broken down as follows:

get all files in these folders
| surround them with quotes (otherwise filenames with spaces would screw up the later steps)
| only consider non-svn files (i’m not interested in the files svn uses to manage things)
| look inside the files for the search string

And here it is:

find app/views public/javascripts -print | awk ‘{print “\””$0″\””}’ | grep -v \.svn | xargs grep -i “ratings”

Obviously this is a bit of a mouthful but once it’s been done it will go into your command memory, and you can get it back by pressing ctrl&r, and starting to type ‘find’.  You may need to press ctrl&r a few more times to get the right command from memory.

Note – wordpress or your browser may change some of the above double quotes to weird web quote characters.  I really fracking hate that.  Anyway, when you paste it over, change all the quotes to normal single or double quotes.

Distinguish between local production mode and server production mode in Rails

I just ran into a problem with production mode. Normally i never run this locally but i decided to use it to run my selenium test battery against, since it’s much faster than dev mode. In production mode on our server we pull our assets in from an amazon asset server, done with this line in production.rb

config.action_controller.asset_host = “http://assets.fakedomain.ir.com&#8221;

In my local version of production i don’t want to do this though, i just want to use what’s in my local public folder. I decided to have a look at ENV which i did by making apache explode 🙂 prior to the above line –

raise “ENV = #{ENV.inspect}”

Quick restart of my local apache, reload the page, and BANG, i can see this:

ENV = {“APACHE_PID_FILE”=>”/var/run/apache2.pid”, “PATH”=>”/usr/local/bin:/usr/bin:/bin”, “LANG”=>”C”, “APACHE_RUN_GROUP”=>”www-data”, “APACHE_RUN_USER”=>”www-data”, “PWD”=>”/etc/apache2/sites-available”, “RAILS_ENV”=>”production”, “HOME”=>”/home/max”, “RAILS_ASSET_ID”=>””}

aha – “HOME”=>”/home/max” – perfect!

So, i changed the line (and removed the raise!) to

config.action_controller.asset_host = “http://assets.fakedomain.ir.charanga.com&#8221; unless ENV[‘HOME’] == “/home/max”

Bingo. Perhaps not the cleverest way of doing it, but it’s safe – for everyone else, including our server, ENV[‘HOME’] will be something else, or perhaps nil. Either way, they’ll use the remote assets site.

Stubbing methods (eg callbacks) in a Machinist blueprint

I had a problem recently where i was using the test object-generating plugin Machinist to make a blueprint for one of my model classes. The model has a before_create filter which references some other data in the database, which didn’t exist in my test version of the db. This before_create filter had nothing to do with the tests i was running and i wanted to just stub it out. I couldn’t work out how, so wrote a wrapper method that looked like this:

def make_music_service(attributes = {})
  music_service = MusicService.make_unsaved(attributes)

That meant that whenever i wanted to make a MusicService in my tests i had to call this method instead.  Pretty clumsy, eh.  But – Machinist creator Pete Yandell came through with a great bit of info on the machinist mailing list – when you’re inside your blueprint, you can reference the object being made with ‘object’.  So, now i can put the stub inside the blueprint, like so:

MusicService.blueprint do
  name     { Faker::Name.name }
  address1 { Faker::Address.street_address }
  address2 { Faker::Address.street_address }
  address3 { Faker::Address.city }
  postcode { Faker::Address.uk_postcode }
  default_password { "password" }

Thanks Pete!

Firebug & jQuery – making other people’s sites better

Was just on a website that had 80 checkboxes (all checked) for various options. I wanted to have only one checked. There’s no ‘Uncheck all’ button. Design Fail! But – open the firebug console and run


Bing, all unchecked. I get a childlike sense of wonder and joy from that, still. If only more things in life could be improved with a little console activity.

jQuery(“#pub.crap”).attr(“ale-count”, 20)

Gotcha with hand-rolled sql in rails associations – eg self.id and has_many

Sometimes in rails associations (eg has_many) you can’t quite do what you want with the existing options and have to do a bit of hand-rolled sql. And, often you want to refer to the object that the association will be called on. BUT – the following doesn’t work. (i’ve used a trivial example here, we wouldn’t normally hand-roll in this case)

has_many :foos, :finder_sql => "select * from foos where bar_id = #{self.id}"

The reason this doesn’t work is that the string has double quotes, which means that #{self.id} will be evaluated when the string is evaluated, which, because this is a class method, is at class load time! That means that self.id will come out as the id of the class – not what we want.

To get around this, use single quotes instead:

has_many :foos, :finder_sql => 'select * from foos where bar_id = #{self.id}'

With single quotes, the #{} block isn’t evaluated and sits there, waiting for the association to be called. When that happens, the finder_sql is combined, and the eval block evaluated, and at that point self IS the current instance, and the association will work. Bear in mind that, because we’re now using single quotes to wrap the string, any single quotes you might have in your sql will now need to be escaped:

has_many :foos, :finder_sql => 'select * from foos where flavour = \'chicken\' and bar_id =#{self.id}'

The shocking truth about rails callbacks! (mind answering my poll?)

Ok, the title’s a bit overly dramatic.  But i recently stumbled upon a feature of rails callbacks (methods called by before_save, after_create, etc) which meant my code was breaking, in a silent way.  After speaking to a few people about it, it seems a lot of people were unaware of this as well.  If you don’t want to read me waffling about my voyage of discovery then would you mind just skipping down to my poll about this?   I’m trying to get a sense of how many other people didn’t know about this either.

Halfway down the api page documenting callbacks ( http://www.railsbrain.com/api/rails-2.2.2/doc/index.html?a=C00000640&name=Callbacks ) , we read this:

Canceling callbacks

If a before_* callback returns false, all the later callbacks and the associated action are cancelled. If an after_*false, all the later callbacks are cancelled. Callbacks are generally run in the order they are defined, with the exception of callbacks defined as methods on the model, which are called last.

Now, i noticed this only because i encountered what seemed to be a bug in my code.  A record wasn’t being saved, and i couldn’t work out why.  It wasn’t failing validations.  If i tried to do it manually in the console, with the same data, i got this odd situation:

=> true

=> false

=> []

WTF? i thought…  after a lot of logging, i managed to work out that it was because i’d added a before_create filter, which set one of the fields to the same as whatever was in a particular field in an associated object, like this:

before_create :set_student_access_to_same_as_music_service</pre>
def set_student_access_to_same_as_music_service
  self.student_access = self.music_service.show_student_access if self.music_service

So, if the user’s music service’s show_student_access field is false, we set the new user’s student_access to false, as a sensible default value.  As a side effect, the method returns false, as that’s the last thing to be evaluated.  And because the callback method returned false, the callback chain is halted and the record isn’t saved.  But, it’s not failing validations so i don’t get to see why.

Now, i’m not saying this is wrong – it’s in the api after all.  I’m just curious as to how many people were also unaware of this – out of the people i’ve spoken to it seems about only half of them knew, which is pretty bad considering it’s a major feature of callbacks, that would be causing unexpected effects all over the place if you weren’t careful about not returning false.  So, would you mind answering my poll, below?

thanks! max

Making acts_as_ferret deal nicely with pluralisation

I recently discovered how to nicely deal with pluralisation in the Rails plugin ‘acts_as_ferret‘ (which we use in our site). When i search for ‘trumpet’, i want to find resources with ‘trumpets’, and vice versa. By default acts_as_ferret doesn’t do this, but it’s easy to add. I found the answer in this post on the acts_as_ferret mailing list/forum:


Anyway, here’s how i used it.  I’m going to assume that anyone reading this has already installed the ferret gem and acts_as_ferret plugin.

Make a new file in lib, called ‘acts_as_ferret_extensions.rb’


module Ferret::Analysis
  class StemmingAnalyzer
    def token_stream(field, text)

Now require this file in the place in your config where you require stuff.  In the default case this would be in config/environment.rb:

#in config/environment.rb
require 'acts_as_ferret_extensions'

Next, change your calls to acts_as_ferret to specify an analyzer: here’s one of mine as an example, from the ‘Asset’ class in this case.

  #in asset.rb (an example from my app)
  acts_as_ferret( :ferret => {:analyzer => Ferret::Analysis::StemmingAnalyzer.new},
                  :fields => { :name => {},
                               :description => {}
                              } )

Now, delete the index and rebuild it – deleting it might not be necessary but i’ve found weird results rebuilding after changing the options to acts_as_ferret: deleting the index is definitely safer.

#at the command line
rm -r index/development/asset/*
#in the console

Now, when you do a search, specify this analyzer again:

#in the console
assets = Asset.find_with_ferret("trumpet", :page => 1,
                                           :per_page => 20,
                                           :analyzer => Ferret::Analysis::StemmingAnalyzer.new)

And that should be it!

Creating a custom “be_valid” matcher for rspec_on_rails

I’ve been discovering the excellent ‘behaviour-driven development’ plugin ‘rspec’ , and its rails-specific cousin (nephew?) ‘rspec_on_rails’ this week.

At the moment i’m testing that validation works properly on some models. Now, rspec already generates a matcher for any predicate method that can be called on an object (eg “object.should be_foo” for “object.foo?.should eql(true)”). I was using this with valid?, which we use on an typical (ie ActiveRecord) model to see if it will pass validation (and therefore be able to be saved to the database). This is generally nicer than trying to save it and seeing if it saved ok.

So, i was writing specs like this

it "should validate with only a name" do
  @foo = Foo.new(:name => "bar")
  @foo.should be_valid

This works, in terms of correctly passing or failing, but it doesn’t give me much information – it just tells me that something fails validation. I could get at the error messages in the test by doing the test as follows:

it "should validate with only a name" do
  @foo = Foo.new(:name => "bar")
  @foo.should be_valid
  @foo.errors.full_messages.should eql([])

but this is a bit tiresome. I’d rather just write “should be_valid”. So, i decided to write a custom matcher that overrides the auto-generated ‘be_valid’ method and tells me why it failed validation (if it does fail). Here’s how i did it. Thanks to the excellent peepcode rspec movies for the inspiration and knowhow!In my spec folder, i made a file called ‘custom_matcher.rb’. In it is a module which will hold any custom rspec matchers i write, though there’s only one at the moment.

module CustomMatchers
  #checks that an AR model validates, by testing error messages from .valid?
  #displays any error messages recieved in test failure output
  class BeValid
    #do any setup required - at the very least, set some instance variables.
    #In this case, i don't take any arguments - it simply either passes or fails.
    def initialize
      @expected = []

    #perform the actual match - 'target' is the thing being tested
    def matches?(target)
      #target.errors.full_messages is an array of error messages produced by the valid? method
      #if valid? is true, it will be empty
      @errors = target.errors.full_messages

    #displayed when 'should' fails
    def failure_message
      "validation failed with #{@errors.inspect}, expected no validation errors"

    #displayed when 'should_not' fails
    def negative_failure_message
      "validation succeeded, expected one or more validation errors"

    #displayed in the spec description if the user doesn't provide one (ie if they just write 'it do' for the spec header)
    def description
      "validate successfully"

    # Returns string representation of the object being tested
    def to_s(value)

  # the matcher method that the user calls in their specs
  def be_valid
#To hook it up, add the following require to spec_helper.rb:
require 'spec/custom_matchers'
#And add the following line to the "Spec::Runner.configure do |config|" section:

This is about as simple as a matcher could be – i don’t take any arguments at all, so the expected result is always an empty array (of errors). The ‘value’ added by this is that i’m now outputting the error messages (if any) in the failure report. Because i’ve provided a description method, i don’t even need to provide a description to my specs if i don’t want to: for example, here’s a test which will fail (and its surrounding description):

describe SchoolSubscriber, ", when we make an empty school_subscriber, " do
  before do
    @ss = SchoolSubscriber.new

  it do
    @ss.should be_valid

And here’s the failure report from rspec:

'SchoolSubscriber , when we make an empty school_subscriber,  should validate successfully' FAILED
validation failed with ["Phone number can't be blank", "First name can't be blank", "Position can't be blank", "School can't be blank", "Last name can't be blank", "Email can't be blank"], expected no validation errors