Newbie - has_many :through case?


#1

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