ActiveRecord’s validations are a convenient way to test and control the state of objects before they hit the database.
Validations are themselves a presumably well-tested part of Ruby on Rails, but their usage in an application significantly changes the behaviour of your objects.
This means they should be spec’d like anything else.
Spec’ing out every single attribute on […]
Category Archives: Ruby on Rails
Quick testing of ActiveRecord validations in RSpec
Optimizing ActiveRecord :includes with :select
I have just read two posts (here and here), both of them discussing the sometimes formidable challenges faced when attempting to optimize ActiveRecord’s performance.
For me, ActiveRecord is easily Rails’ weakest link. Arguably, part of the problem is its ease of use. The object-oriented syntax means many developers find it far too easy to avoid opening […]
Storing Rails’ Flash object throughout multiple redirects
I am occasionally faced with the sometimes awkward problem of how to store a Flash message during more than one redirect. The problem is that the flash is usually cleared after one redirect, but sometimes you want to redirect more than once, but retain some flash message to display on the final destination.
These less than […]
Extending restful_authentication
Most websites need to offer users several levels of access. Sometimes it’s as simple as a straight choice between logged in, or logged out - something which both restful_authentication and acts_as_authenticated handle competently.
As a site grows, however, so does the likelihood that further levels of authentication will need to be checked. Now, there’s an entire […]
Haml scaffolding for Rails
Haml is a masterstroke - since moving over to it, writing markup has ceased being the chore it was. In fact, it’s so logical and intuitive, and saves so much time, I often wonder how I coped without for so long.
The one irritation is that Rails does not yet support Haml in its scaffold generator, […]
Object.too_clever_by_half? rescue nil
Monkey-patching proposals have abounded in the Ruby community recently - including, but not limited to Object#andand, Object#_? and SafeNil and Object#method_.
The underlying aim of all seems to be to keep code as DRY as possible. Inline validity checking is considered too ugly and verbose, as in the following snippet:
@person.name unless @person.nil?
Most of the proposals, however, […]
Automate your database content migrations
Rails manages database schema migrations with the minimum of fuss, but the prospect of copying database content between a number of development and deployment machines has long filled me with a faint sense of dread.
Usually, I have resorted to the cumbersome process of producing and tarball-ing an SQL dump of the data, transferring it to […]