# STI, has many, and Controllers

**URL:** <https://www.ruby-forum.com/t/sti-has-many-and-controllers/175481>\
**Category:** Rails\
**Created:** [September 14, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/sti-has-many-and-controllers/175481 "2009-09-14T15:35:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [September 14, 2009, 3:35pm UTC](https://www.ruby-forum.com/t/sti-has-many-and-controllers/175481/1 "2009-09-14T15:35:51Z")

</div>

Hi,

I’m trying to change an app I have that right now has this relationship:

Team has many Matches

I also have the appropriate Team and Match controllers, and routes:

map.resources :matches  
map.resources :teams, :has\_many =\> :matches

I need to add two different types of matches - qualifications and  
finals, so I thought STI would  
be helpful. So I set up:

class Match \< ActiveRecord::Base  
class Qualification \< Match  
belongs to team  
class Final \< Match  
belongs to team

and change the Team model to have:

has\_many :qualifications  
has\_many :finals

I added the “type” column to the matches table to make the magic work.  
Now, through the console to  
do some testing/investigation, I am able to add qualifications and  
finals to a team.

Now I’m trying to modify the controllers and views to be able to add a  
new qualification or final  
match to a team. Do I now have to add two new controllers and pretty  
much duplicate the Match  
controller in each? To enter match data for a team, I created a link  
using:

new\_team\_match\_path(team)

I think I would need to change my routes to include:

map.resources :matches  
map.resources :qualifications  
map.resources :finals

map.resources :teams, :has\_many =\> :qualifications  
map.resources :teams, :has\_many =\> :finals

Now I’m not so sure this is the way to go. Any hints or comments would  
be extremely helpful!

Thanks

---

<div class="post-metadata">

**Author:** ![johnt519](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cab0a1/32.png) [@johnt519](https://www.ruby-forum.com/u/johnt519)\
**Post date:** [September 14, 2009, 9:12pm UTC](https://www.ruby-forum.com/t/sti-has-many-and-controllers/175481/2 "2009-09-14T21:12:17Z")

</div>

John T. wrote:

> Hi,
> 
> I’m trying to change an app I have that right now has this relationship:
> 
> Team has many Matches
> 
> I also have the appropriate Team and Match controllers, and routes:
> 
> map.resources :matches  
> map.resources :teams, :has\_many =\> :matches
> 
> I need to add two different types of matches - qualifications and  
> finals, so I thought STI would  
> be helpful.

Well, I think I’m just going to add a match\_type field to the matches  
model/table and just add the right stuff to make that work. Trying to  
figure out the routes and controllers just isn’t working.

thanks anyway.
