Rails has_many associations -


considering following models, how can select notes of students owned user? also, models ok?

class student < activerecord::base     has_many :student_notes     has_many :notes, :through => :student_notes      has_many :relationships     has_many :users, :through => :relationships end  class note < activerecord::base     has_many :student_notes     has_many :students, :through => :student_notes end  class studentnote < activerecord::base     belongs_to :student     belongs_to :note end  class user < activerecord::base     has_many :relationships     has_many :students, :through => :relationships end  class relationship < activerecord::base   belongs_to :student   belongs_to :user end 

thanks in advance!

you simplify models cutting off studentnoteand relationship , using has_and_belongs_to_many association instead.

to select notes of students owned user, add has_many :notes, :through => :students user model

your models should this:

class student < activerecord::base     has_and_belongs_to_many :notes     has_and_belongs_to_many :users end  class note < activerecord::base     has_and_belongs_to_many :students end  class user < activerecord::base     has_and_belongs_to_many :students     has_many :notes, :through => :students end 

and select notes of students owned user way:

some_user.notes


Comments