# Newbie - has\_many :through case?

**URL:** <https://www.ruby-forum.com/t/newbie-has-many-through-case/159920>\
**Category:** Rails\
**Created:** [February 10, 2009, 1:04am UTC](https://www.ruby-forum.com/t/newbie-has-many-through-case/159920 "2009-02-10T01:04:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![gamericax](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/65b543/32.png) [@gamericax](https://www.ruby-forum.com/u/gamericax)\
**Post date:** [February 10, 2009, 1:04am UTC](https://www.ruby-forum.com/t/newbie-has-many-through-case/159920/1 "2009-02-10T01:04:16Z")

</div>

hi,

i’m a newbie digging ROR for a week (and loving it i might add) and  
trying to get a project done at the same time. i’m more used to  
direct SQL so trying to get hang of active record asssociations and  
figure out what is done for me and what is not by script/generate  
model and db:migrate.

in a nutshell, i have 3 core models where users enter competitions  
with submissions.

entities:

- competition
- user
- submission

will need to:  
pull all submissions by competition  
pull all users by competition  
pull all competitions by user  
pull all submissions by user

1. is this a case of has\_many :through like so?

class Competition \< ActiveRecord::Base  
has\_many :submissions  
has\_many :users, :through =\> :submission  
end

class Submission \< ActiveRecord::Base  
belongs\_to :competition  
belongs\_to :submission  
end

class User \< ActiveRecord::Base  
has\_many :submissions  
has\_many :competitions, :through =\> :submissions  
end

1. do i create joining tables and define FKs by updating migration  
script manually? if there are materials on step instructions you can  
point me to, that will be great too!

2. if i need to add polymorphic behaviors(e.g. commentable and  
rateable) to each of these models, do i just pile up the association  
definitions? after adding on media and various type/category models,  
this probably will get pretty long and not sure whether cache/memory/  
performance situation will become unmanageable. An example of  
relatively complex association definition will give me a good idea.

thx for reading.

-ji
