# Getting the through items from a has\_many :through :uniq relationship

**URL:** <https://www.ruby-forum.com/t/getting-the-through-items-from-a-has-many-through-uniq-relationship/117617>\
**Category:** Rails\
**Created:** [October 18, 2007, 6:25pm UTC](https://www.ruby-forum.com/t/getting-the-through-items-from-a-has-many-through-uniq-relationship/117617 "2007-10-18T18:25:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_Grimm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f19dbf/32.png) [@Andrew\_Grimm](https://www.ruby-forum.com/u/Andrew_Grimm)\
**Post date:** [October 18, 2007, 6:25pm UTC](https://www.ruby-forum.com/t/getting-the-through-items-from-a-has-many-through-uniq-relationship/117617/1 "2007-10-18T18:25:06Z")

</div>

Getting the through items from a has\_many :through :uniq relationship

In a nutshell, I have been using the has\_many :through :uniq  
relationship, and I want a DRY way to list the join model objects.

Currently, my objects include taxon objects, toxin objects,  
data\_object objects, and taxon\_toxin\_citation objects (“citation” is  
another word for data\_object). A taxon has many data objects, has many  
taxon\_toxin\_citations, and has many toxins through  
taxon\_toxin\_citations. As well as a taxon causing many toxins, a toxin  
can be caused by many taxa. And there are many taxon\_toxin\_citations  
linking a given taxon with a given toxin. (If anyone is interested in  
project, it is currently hosted at [Google Code Archive - Long-term storage for Google Code Project Hosting.](http://code.google.com/p/habevent/)  
)

If you want something more familiar, replace taxon with actor, toxin  
with movie, data\_object with character, with the possibility that an  
actor may appear in a movie as more than one character.

When I’m displaying information about a taxon, I’d like to display  
information about the toxins associated with the taxon, and for each  
toxin, I’d like to display the citations saying that the taxon is  
associated with the toxin.

Getting the list of toxins itself is easy. I have in my Taxon model

has\_many :data\_objects  
has\_many :taxon\_toxin\_citations  
has\_many :toxins, :through =\> :taxon\_toxin\_citations, :uniq=\> TRUE

so all I have to do is ask for taxon.toxins

But to get the list of citations/data objects linking a specific taxon  
with each toxin is a bit trickier. I currently have

toxin\_citations\_for\_taxon = {}  
taxon.toxins.each do |toxin|  
taxon\_toxin\_citations =  
TaxonToxinCitation.find\_all\_by\_taxon\_id\_and\_toxin\_id(taxon, toxin)  
toxin\_citations\_for\_taxon[toxin] = []  
taxon\_toxin\_citations.each do |taxon\_toxin\_citation|  
toxin\_citations\_for\_taxon[toxin] \<\<  
taxon\_toxin\_citation.data\_object  
end  
end

which isn’t very DRY, and means that I have to return another  
variable.

taxon.taxon\_toxin\_citations won’t help, as it’d mention data\_objects  
that link a taxon with any toxin, not just the specific one I’m  
interested in. Likewise, toxin.taxon\_toxin\_citations won’t help, as  
it’d mention data\_objects that link any taxon with the specific toxin.

A few other people have asked this question, including in the comments  
section of  
[http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off](http://blog.hasmanythrough.com/2006/4/20/many-to-many-dance-off)  
and “Please clarify” in  
[http://wiki.rubyonrails.org/rails/pages/ThroughAssociations](http://wiki.rubyonrails.org/rails/pages/ThroughAssociations)  
. There have been some attempts to reply, but they haven’t really  
answered the question.

Is there currently a better way to find the join model objects?

Thanks,

Andrew Grimm
