# Has\_many :through and has\_one association

**URL:** <https://www.ruby-forum.com/t/has-many-through-and-has-one-association/89129>\
**Category:** Rails\
**Created:** [March 17, 2007, 4:23pm UTC](https://www.ruby-forum.com/t/has-many-through-and-has-one-association/89129 "2007-03-17T16:23:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [March 17, 2007, 4:23pm UTC](https://www.ruby-forum.com/t/has-many-through-and-has-one-association/89129/1 "2007-03-17T16:23:54Z")

</div>

Can’t get the following to work:

class Forum \< ActiveRecord::Base  
has\_many :topics  
has\_many :recent\_posts, :through =\> :topics, :source =\> :last\_post,  
:order =\> ‘posted\_at DESC’  
end

class Topic \< ActiveRecord::Base  
has\_one :last\_post, :class\_name =\> ‘Post’, :order =\> ‘posted\_at DESC’  
end

class Post \< ActiveRecord::Base  
belongs\_to :topic  
end

Forum.find(:first).offers

I get the following error:

ActiveRecord::HasManyThroughSourceAssociationMacroError:  
ActiveRecord::HasManyThroughSourceAssociationMacroError  
from  
c:/InstantRails-1.0/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/reflection.rb:181:in  
`check_validity!' from c:/InstantRails-1.0/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active_record/associations/has_many_through_association.rb:6:in`initialize’  
from  
c:/InstantRails-1.0/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.2/lib/active\_record/associations.rb:876:in  
`offers’  
from (irb):93

Any clues?

Cheers,

~ Mark D.

---

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [March 17, 2007, 4:24pm UTC](https://www.ruby-forum.com/t/has-many-through-and-has-one-association/89129/2 "2007-03-17T16:24:46Z")

</div>

> Forum.find(:first).offers

Sorry I meant:

Forum.find(:first).recent\_posts

---

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [March 17, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/has-many-through-and-has-one-association/89129/3 "2007-03-17T18:11:35Z")

</div>

I would suggest fully qualifying the :order fields as one thing to  
try. I do not see anything obvious that is wrong with the models.

Can you post the migrations as well? It may be a miss-match between  
the models and the tables.

Michael

On Mar 17, 8:24 am, Mark D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![mdaoust](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/dbc845/32.png) [@mdaoust](https://www.ruby-forum.com/u/mdaoust)\
**Post date:** [March 17, 2007, 8:46pm UTC](https://www.ruby-forum.com/t/has-many-through-and-has-one-association/89129/4 "2007-03-17T20:46:23Z")

</div>

It seems the has\_one assoc. on the Topic model is why this doesn’t work.  
It returns a single item as opposed to an array containing a single  
item.

I ended up doing this:

class Forum \< ActiveRecord::Base

class Forum \< ActiveRecord::Base  
has\_many :topics  
has\_many :recent\_topics, :class\_name =\> ‘Topic’, :finder\_sql =\>  
‘SELECT DISTINCT topics.\* FROM (’ +  
'SELECT topics.\* ’ +  
'FROM forums ’ +  
'INNER JOIN topics ON forums.id = topics.forum\_id ’ +  
'INNER JOIN posts ON topics.id = posts.topic\_id ’ +  
'WHERE forums.id = #{id} ’ +  
‘ORDER BY posts.posted\_at DESC’ +  
‘) AS topics’

has\_many :recent\_posts, :through =\> :topics, :source =\> :last\_post,  
:order =\> ‘posted\_at DESC’  
end

class Topic \< ActiveRecord::Base  
has\_many :last\_post, :class\_name =\> ‘Post’, :order =\> ‘posted\_at  
DESC’, :limit =\> 1  
end

class Post \< ActiveRecord::Base  
belongs\_to :topic  
end

As you can see it’s not very pretty. In addition I had to do some pretty  
crazy custom finder\_sql to add the ‘recent\_topics’ assoc.

After several hours I gave up trying to find a DRY solution. Any  
improvements welcomed!
