Rails query using HMTH and multiple join models -


i using rails 3 , wanted classes student has access based upon model below

class student     has_many :students_levels     has_many :levels, :through => :students_levels     end      class class     has_many :classes_levels     has_many :levels, :through => :classes_levels      end      class level     has_many :students_levels     has_many :classes_levels     end      class studentslevel      belongs_to :students     belongs_to :levels     end     class classeslevel     belongs_to :classes     belongs_to :levels     end 

i came query below didn't think seemed best rails way things , wanted additional suggestions. thx

class.where(:id => (classeslevel.where(:level_id => student.find(1).levels))) 

i want add instance method student , thinking there better way doing has many through.

i quite did not understand whole logic behind class structure. why not connecting students directly class? , how class can have many levels. mean if have math1 , math2, different classes, right? or have math1,2,3?

well, anyway, here's solution if want use current assosiations, hope suites needs:

 class student        ...  def available_classes     class.find(:all,        :include => {:levels => {:students_levels => :student}},        :conditions => ["students.id = ?", self.id])   end 

and sorry, still in rails 2.x format...


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -