# How does Rails handle many to many relationships?

**URL:** <https://www.ruby-forum.com/t/how-does-rails-handle-many-to-many-relationships/73898>\
**Category:** Rails\
**Created:** [October 27, 2006, 9:38pm UTC](https://www.ruby-forum.com/t/how-does-rails-handle-many-to-many-relationships/73898 "2006-10-27T21:38:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [October 27, 2006, 9:38pm UTC](https://www.ruby-forum.com/t/how-does-rails-handle-many-to-many-relationships/73898/1 "2006-10-27T21:38:28Z")

</div>

I’m trying to show a relationship for a user having many tasks. I’ve  
noticed the :has\_many statement in Rails am not sure what “automagic”  
that  
creates for you. In .NET, for instance, I would create a bridge table  
that  
would link user\_id to task\_id and run queries off of that. For Rails, I  
get  
a feeling some of this is done for you by using :has\_many. Can anyone  
explain some of this to me? Or am I crazy and I must build some bridge  
table and run queries against it?

–  
Chris S.  
removed\_email\_address@domain.invalid  
[http://compiledmonkey.wordpress.com](http://compiledmonkey.wordpress.com)

---

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [October 27, 2006, 9:42pm UTC](https://www.ruby-forum.com/t/how-does-rails-handle-many-to-many-relationships/73898/2 "2006-10-27T21:42:28Z")

</div>

you need to create a task\_user table with columns task\_id and user\_id

after the associations are set in the models, rails knows how to get the  
objects without querying the bridge table. for example, by calling  
directly  
@user.tasks is possible

On 10/27/06, Chris S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Chris S.  
> removed\_email\_address@domain.invalid  
> [http://compiledmonkey.wordpress.com](http://compiledmonkey.wordpress.com)
> 
> >

–  
Heri R.  
[http://sprinj.com](http://sprinj.com)

---

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [October 27, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/how-does-rails-handle-many-to-many-relationships/73898/3 "2006-10-27T21:52:30Z")

</div>

And just leave the “id” columns as defaults for the task and user table?

On 10/27/06, Heri R\> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > noticed the :has\_many statement in Rails am not sure what “automagic” that
> 
> –  
> Heri R.  
> [http://sprinj.com](http://sprinj.com)
> 
> >

–  
Chris S.  
removed\_email\_address@domain.invalid  
[http://compiledmonkey.wordpress.com](http://compiledmonkey.wordpress.com)
