Rails relation select

Posted by Dimitar Vouldjeff on Stack Overflow See other posts from Stack Overflow or by Dimitar Vouldjeff
Published on 2010-03-19T08:39:28Z Indexed on 2010/03/19 8:41 UTC
Read the original article Hit count: 300

Filed under:
|

Hi, I have the following models:

class User < ActiveRecord::Base
    has_many :results, :dependent => :destroy
    has_many :participants, :dependent => :destroy
    has_many :courses, :through => :participants
  end



class Course < ActiveRecord::Base
    has_many :tests, :dependent => :destroy
    has_many :participants, :dependent => :destroy
    has_many :users, :through => :participants
  end



class Result < ActiveRecord::Base
    belongs_to :test
    belongs_to :user
  end



class Test < ActiveRecord::Base
    belongs_to :course
    has_many :results, :dependent => :destroy
  end

The Idea is that a user has_and_belongs_to_many courses, the course has_many tests, and every test has_and_belongs_to_many users (results). So what is the best query to select every Result from a single Course (not test), and also the query to select every Result from a single Course, but from one user.

Thanks!

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about activerecord