2011-10-31 4 views
1

가 레일에서 4 개 모델을 상상 레일의 속성 3.1중첩이

class Student < ActiveRecord::Base 
    has_many :memberships 
    has_many :courses, :through => :memberships 
    has_many :tests, :through => :courses 
end 

class Membership < ActiveRecord::Base 
    belongs_to :student 
    belongs_to :course 
end 

class Course < ActiveRecod::Base 
    has_many :tests 
    has_many :students, :through => :memberships 
end 

class Test < ActiveRecord::Base 
    belongs_to :course 
end 

어떻게 I 출력 학생의 향후 테스트 의 (날짜 즉) 정렬 된 목록 (I 상당히 간단한 대답이 같은데요, 하지만 난 잠시 동안 헛된)에 노력했습니다

내 추측은 뭔가 같다 :

@upcomingTests = @currstudent.tests.sort_by &:testDateTime 

하지만, 빈 배열을 반환 것으로 보인다

+0

최상의 시도와 결과를 게시하십시오. –

답변

0

먼저 "코스"모델에 약간의 오류가 있습니다. 그것은 "belongs_to : student"가 필요합니다. 사용자가 만든 및 외부 키를 채워 한 후

class Course < ActiveRecod::Base 
    has_many :tests 
    has_many :students, :through => :memberships 
    belongs_to :student 
end 

, 당신은 당신의 테스트 모드에 간단한 named_scope를 만들 수 있습니다

named_scope :ordered, :order => "created_at DESC" 

을 당신이 원하는 목적지 그런 다음이에서 액세스 단지 문제가있다 :

@ordered_tests = @student.tests.ordered 
관련 문제