# Multiple habtm joins on the same two tables

**URL:** <https://www.ruby-forum.com/t/multiple-habtm-joins-on-the-same-two-tables/101697>\
**Category:** Rails\
**Created:** [August 7, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/multiple-habtm-joins-on-the-same-two-tables/101697 "2006-08-07T15:13:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [August 7, 2006, 3:13pm UTC](https://www.ruby-forum.com/t/multiple-habtm-joins-on-the-same-two-tables/101697/1 "2006-08-07T15:13:56Z")

</div>

i have a taskmanager type of project that i am working on, where i need  
to separate joins on the same two tables. i need one join for users that  
are assigned to the task, and another join for who has read the task, so  
they can tell which ones are new.

any suggestions on the best way to accomplish this?

thanks

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [August 7, 2006, 3:33pm UTC](https://www.ruby-forum.com/t/multiple-habtm-joins-on-the-same-two-tables/101697/2 "2006-08-07T15:33:31Z")

</div>

Hi Josh,

I think you might be able to model this with has\_many :through either  
one  
Model (e.g. UserTaskRelation with two flags: ‘read’ and ‘assigned’ in  
it) or  
two Models ‘Reading’, ‘Assignation’  
User has\_many :tasks, :through =\> :reading  
User has\_many :tasks, :through =\> :assignation

with the second approach you are able with the standard created\_at,  
updated\_at to tell when a user did a reading or has been assigned to a  
task.

Cheers,  
Jan

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [August 7, 2006, 3:37pm UTC](https://www.ruby-forum.com/t/multiple-habtm-joins-on-the-same-two-tables/101697/3 "2006-08-07T15:37:35Z")

</div>

Jan P. wrote:

> Hi Josh,
> 
> I think you might be able to model this with has\_many :through either  
> one  
> Model (e.g. UserTaskRelation with two flags: ‘read’ and ‘assigned’ in  
> it) or  
> two Models ‘Reading’, ‘Assignation’  
> User has\_many :tasks, :through =\> :reading  
> User has\_many :tasks, :through =\> :assignation
> 
> with the second approach you are able with the standard created\_at,  
> updated\_at to tell when a user did a reading or has been assigned to a  
> task.
> 
> Cheers,  
> Jan

thanks for the help

what about the database schema? should the link tables be reading\_tasks  
and assignation\_tasks for those conventions to work?

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [August 8, 2006, 6:14am UTC](https://www.ruby-forum.com/t/multiple-habtm-joins-on-the-same-two-tables/101697/4 "2006-08-08T06:14:12Z")

</div>

Jan P. wrote:

> Hi Josh,
> 
> I think you might be able to model this with has\_many :through either  
> one  
> Model (e.g. UserTaskRelation with two flags: ‘read’ and ‘assigned’ in  
> it) or  
> two Models ‘Reading’, ‘Assignation’  
> User has\_many :tasks, :through =\> :reading  
> User has\_many :tasks, :through =\> :assignation
> 
> with the second approach you are able with the standard created\_at,  
> updated\_at to tell when a user did a reading or has been assigned to a  
> task.

This is sort of backwards. I think you want to have one join model and  
two associations going through it.

User:  
has\_many :assignments  
has\_many :assigned\_tasks, :through =\> :assignments, :class\_name =\>  
“Task”  
has\_many :read\_tasks, :through =\> :assignments, :class\_name =\> “Task”

You can read more about this approach here:  
[http://blog.hasmanythrough.com/articles/2006/05/06/through\_gets\_uniq](http://blog.hasmanythrough.com/articles/2006/05/06/through_gets_uniq)

–  
Josh S.  
[http://blog.hasmanythrough.com/](http://blog.hasmanythrough.com/)

---

<div class="post-metadata">

**Author:** ![jjkiesch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc79bd/32.png) [@jjkiesch](https://www.ruby-forum.com/u/jjkiesch)\
**Post date:** [August 8, 2006, 1:34am UTC](https://www.ruby-forum.com/t/multiple-habtm-joins-on-the-same-two-tables/101697/5 "2006-08-08T01:34:31Z")

</div>

Hi Josh,

no for the through association you don’t need a solely join table of the  
kind of comments\_posts. Instead you’ve got a full featured model (and  
its  
underlying table like ‘readings’) wich might grow with your application  
and  
it’s growing needs and is even able to be handled in the great way of  
the so  
called restful or crud controllers. Do a little research on ‘has\_many  
:through’ as well as restful and you’ll get the idea. HABTM has it’s use  
cases but a full ‘through-model’ has so many advantages especially for  
cases  
like yours.

Cheers,  
Jan
