This project is archived and is in readonly mode.

#2719 ✓resolved
sbwoodside

has_many :through should work if the join model uses has_one relationship

Reported by sbwoodside | May 26th, 2009 @ 05:07 AM

Let's say you have User has_many Posts, and each Post has_one Photo. If you try to set a has_many :photos, :through :posts and then access user.photos you will get something like this:

ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source reflection macro :has_one for has_many :photos, :through => :posts.  Use :source to specify the source reflection.

Besides the fact that the error message isn't too helpful, it appears that this isn't supported in Rails 2.3.2 or previously. It would be awfully nice to be able to, for example, list all of the Photos for a certain User, etc.

There is an old bug in Rails Trac for this, which can no longer be modified.

It's not an uncommon problem: http://railsforum.com/viewtopic.php?pid=86321#p86321 and http://railsforum.com/viewtopic.php?id=2295 and http://railsforum.com/viewtopic.php?id=265#p4120 and http://railsforum.com/viewtopic.php?pid=6388#p6388

Is there a suggested workaround? (Aside from hacking SQL...)

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Referenced by

Pages