Ruby Hash

One of the popular feature’s of Ruby’s Hash object is that you can specify a default value for entries in the hash. For example, a common use case is to count the frequency of objects in an array. hash = Hash.new %w( orange red blue orange ).each do |colour| hash[colour] ||= 0 hash[colour] += 1 […]

Tags: Ruby

Faster CSV Imports with Rails

On my current project we deal with quite a few CSV imports. While writing a basic CSV import is reasonably straightforward, we ran into performance issues when we started scaling the solution. Basically we could import 3,000 users in around 30 minutes (the domain model being rather complicated), but we needed to scale this solution […]

Tags: Rails, Ruby

Using Pluralize in your Models in Rails

One of the really nice helpers in Rails is pluralize. It basically provides a really simple way of finding the plural form of a word, courtesy of ActionView::Helpers::TextHelper. pluralize(1, ‘person’) # => 1 person pluralize(2, ‘person’) # => 2 people pluralize(3, ‘person’, ‘users’) # => 3 users If you want access to the pluralize functionality […]

Tags: Rails, Ruby

Rails File Size methods on Fixnum

As a Rails developer I have often seen the shorthands for specifying different dates using the built-in methods on Fixnum. For example, if you want to specify the date for 5 days ago, you can simply use: 5.days.ago However, what I didn’t know was that there is a similar shorthand for specifying file sizes – […]

Tags: Rails, Ruby

Convert seconds to hours:minutes:seconds in Ruby

Today I was working on an analytics report where the time a user spent on the site was being reported in seconds. I wanted to convert this into a nicer format – hours:minutes:seconds. I had hoped that there was a nice helper in Rails to accomplish this, but unfortunately there isn’t. Luckily simple math can […]

Tags: Ruby

Allow Users to Remove Uploaded Images with Paperclip

Allowing users to upload images is pretty straightforward in Rails – especially if you’re using the excellent Paperclip gem. I’ve blogged before about how easy it is to configure Paperclip to upload images to S3. However, there are no nice examples (that I could find) of allowing your users to remove images that are already […]

Tags: Rails, Ruby

Tips for Implementing Emails in Rails

Sending emails from your Rails application is a pretty common requirement. User signup, forgotten password, order creation, nightly reports – they all require emails. This is pretty straightforward in Rails – we already have the concept of mailers (with views) – you simply need to configure your mail service and you’re done! However, every time […]

Tags: Rails, Ruby

RSpec Best Practices

I’ve been using RSpec on and off for the past 2 years. I’ve learnt that it’s easy to write tests that are bloated, slow and don’t offer any value. I’ve also learnt a few tricks and best practices that will make your RSpec life a bit easier. Some of these might be straightforward while others […]

Tags: Rails, RSpec, Ruby

Simplify your Ruby code with tap

I only recently discovered the tap method, which was added in Ruby 1.9. Basically, if you ever find yourself writing code like this: def user_mapping mapping = {} User.all.each do |user| mapping[user.email] = user end mapping end You can now simplify this code using the tap method: def user_mapping {}.tap do |mapping| User.all.each do |user| […]

Tags: Ruby

Integrating Solr Search with Spree

For the past 2 months I’ve been working on an e-commerce website where we’ve been using Spree. The built-in searching with Spree is pretty decent and simply builds SQL queries to execute against the database. While this is a great starting point we quickly got to a point where we needed to do more advanced […]

Tags: Rails, Ruby, Spree