# Associations: Am I thinking right?

**URL:** <https://www.ruby-forum.com/t/associations-am-i-thinking-right/156886>\
**Category:** Rails\
**Created:** [January 4, 2009, 9:55pm UTC](https://www.ruby-forum.com/t/associations-am-i-thinking-right/156886 "2009-01-04T21:55:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@tim](https://www.ruby-forum.com/u/tim)\
**Post date:** [January 4, 2009, 9:55pm UTC](https://www.ruby-forum.com/t/associations-am-i-thinking-right/156886/1 "2009-01-04T21:55:35Z")

</div>

Hello everyone,

I just read the entire Associations page on the RoR Guide, and I’m  
wanting to see if I can run it by the pros to make sure I have it down  
right for my particular project. As a first test project I’m doing a  
simple Issue Tracker.

The tracker has the following models:

Projects  
Tickets  
Milestones  
Components  
Types  
Severities

Each Project will have many Tickets  
Each Ticket will have one of the following, Milestone, Component,  
Type, Severity

From reading the RoR Association Guide I have guessed that this is the  
best method using Milestone as the example:

Project: has\_many: tickets, has\_many: milestones, :through =\> tickets  
Ticket: belongs\_to: project, belongs\_to: milestone  
Milestone: has\_many: tickets, has\_many: projects, :through =\> tickets

Would this be the correct associations for have a Project will have  
multiple tickets, and each ticket will have exactly one milestone  
reference?

The thing that has me confused is that each project can have multiple  
milestone, ie: Milestone 1, Milestone 2, etc…but these milestones  
will be related to the Project, so in the actual table there may be 5  
instances of Milestone 1 but with a different project id.

I’m so lost, lol.

---

<div class="post-metadata">

**Author:** ![tim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@tim](https://www.ruby-forum.com/u/tim)\
**Post date:** [January 4, 2009, 10:00pm UTC](https://www.ruby-forum.com/t/associations-am-i-thinking-right/156886/2 "2009-01-04T22:00:20Z")

</div>

I would ditch the :through association here and have:

Milestone  
belongs\_to :project  
has\_many :tickets

Ticket  
belongs\_to :project  
belongs\_to :milestone

Project  
has\_many :milestones  
has\_many :tickets

This might seem like you’re doubling up, but this best represents the  
real world model. A project has milestones and tickets can belong to  
these milestones. Tickets may also exist outside of milestones.

With your initial model, you’d need to create tickets in order to  
create milestones - which is not correct.

Hope that makes sense!

Steve

---

<div class="post-metadata">

**Author:** ![tim](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ce73a5/32.png) [@tim](https://www.ruby-forum.com/u/tim)\
**Post date:** [January 4, 2009, 10:05pm UTC](https://www.ruby-forum.com/t/associations-am-i-thinking-right/156886/3 "2009-01-04T22:05:38Z")

</div>

Steve,

That makes perfect sense, I’m new to this but something about the way  
I was looking at it didn’t seem right, didn’t realize you could do  
what you did, but it makes perfect sense!!!

Thanks for the quick reply!
