Forum: Ruby on Rails Newbie - has_many :through case?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
gamericax (Guest)
on 2009-02-10 02:04
(Received via mailing list)
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


2. 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!

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