Author Archives: peter

Rails Gotcha: Mocking/Stubbing on an Associated ActiveRecord Object

In RSpec it is still not possible to have mocks or stubs on all instances of a class, although this has been suggested and it is a feature of the Mocha framework. Stubbing or mocking an object returned by an ActiveRecord belongs_to association (i.e. article.author) won’t do what you expect. Instead of mocking the associated [...]

Rails Tip: Safe Usage of URI.parse

As documented already by Doug URI.parse will thrown an exception if your URL has a trailing space. It also throws an exception on invalid URLs in general. To avoid having URI.parse bomb on your pages you can use a construct like this:

Keep Your I18n Translation Files Tidy

I added the Rake task translate:remove_obsolete_keys that lets you remove all obsolete translations from your YAML files. How is a translation rendered obsolete? Suppose you are translating from english to a number of other languages. Over time you may end up removing a number of I18n keys from the english translation file as your application [...]

Configuring Exception Handling in RSpec and Controller Tests

The ActionController::Rescue module in Rails defines a number of exceptions that will trigger a 404 response, namely ActionController::RoutingError, ActionController::UnknownAction, and ActiveRecord::RecordNotFound. However if you trigger one of those exceptions in a controller test (Test::Unit or Rspec) then rescue and display of the 404 page will not happen and instead the exception will just be propagated. [...]

acts_as_bitfield Plugin now Radio Button Compatible

I’ve patched the acts_as_bitfield plugin to recognize “true” as true so that now instead of having to write:
<%= f.radio_button(:email_activated, true, :checked => (”checked” if @item.email_activated?)) %> Yes
<%= f.radio_button(:email_activated, false, :checked => (”checked” unless @item.email_activated?)) %> No
you can omit the checked argument just like with ActiveRecord boolean attributes:
<%= f.radio_button(:email_activated, true) %> Yes

<%= f.radio_button(:email_activated, false) %> No

Rails I18n and 404/500 Error Pages

If you have a multilingual site it makes sense for your 404 and 500 error pages to be multilingual as well. Rails supports multilinguality by serving up error pages with a path like public/500.sv.html, where sv is the locale. In the case where Rails itself is fundamentally broken or fails to respond your webserver will [...]

Rails Development Database Setup Without Migrations

I think a lot of Rails developers have been using the db:migrate Rake task to setup new development databases. The problem with this is that over time migrations tend to grow out of sync with the code, especially if you use model classes in your migrations (which in some cases can be very convenient and [...]

Rails bug fix: ActiveRecord::Base#exists? now compatible with include scopes

I was using a default scope for a model with :include and :order options and ran into an issue with the exists? method. We have now refactored the exists? method to invoke find_initial which handles scopes correctly. Thanks Michael for committing this!

Rails Gotcha: Global Methods can Cause ActiveRecord Attribute Methods to Never be Defined

I just ran into a sort of edge case gotcha in the interaction between the acts_as_taggable_on Rails plugin and RSpec. It turns out both libraries define a context method. In the RSpec case it is a global method (defined in Object) and in the plugin case it is the Tagging model context attribute. As you [...]

Rails Gotcha: Eager Loading Application Classes to Make STI Work

Recent versions of Rails (2.2 and later I think) will eager load all application Ruby classes when booting up the server. However, because of issues with migrations this is not done if you are executing a Rake task that depends on the :environment task (such as db:migrate). It is also not done if cache_classes is [...]