Forum: Ruby on Rails Associations: Am I thinking right?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
5c504a3534ec5a2e7665ba2f4be2b62c?d=identicon&s=25 Tim (Guest)
on 2009-01-04 21:55
(Received via mailing list)
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:


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

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.
6d57db44c5a4e0721cc7f78cf0ce308a?d=identicon&s=25 Stephen Bartholomew (steveb)
on 2009-01-04 22:00
(Received via mailing list)
I would ditch the :through association here and have:

  belongs_to :project
  has_many :tickets

  belongs_to :project
  belongs_to :milestone

  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!

5c504a3534ec5a2e7665ba2f4be2b62c?d=identicon&s=25 Tim (Guest)
on 2009-01-04 22:05
(Received via mailing list)

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!
This topic is locked and can not be replied to.