Space Vatican

Ramblings of a curious coder

Watch Out for Has_one :through and :include

I’ve seen enough people confused about this that it’s probably worth broadcasting this slightly more widely. In a nutshell, :include of has_one :through associations is broken in Rails 2.1. Rails 2.1.1 and higher are fixed.

As you may recall, :include takes ones of two different paths.

In the first (the default) Rails loads parent records first, and will then load all the child record of all those parent ones in one go. Unfortunately in Rails 2.1 this isn’t done quite right and the net effect is that the associations are loaded normally and then preloaded. This was fixed as of fdeeeaea and is included in Rails 2.1.1

In the second case Rails generates appropriate join statements. This is used when you have conditions or orders on the joined tables and also if you have a count or a sum which uses columns from the joins tables. This just plain wasn’t implemented, so it was being treated as a plain old has_one which results in an angry message from the database about you referencing a non existant column name. This was fixed as of bff0f5fb and like the previous fix is in Rails 2.1.1