ruby on rails - Best way to get a list of parent IDs -
i have these models in rails app:
class course of study < activerecord::base has_many :topics end class topic < activerecord::base has_many :parts belongs_to :course end class part < activerecord::base has_many :quizzes belongs_to :topic end class quiz < activerecord::base has_many :quiz_submissions belongs_to :part end class quizsubmission < activerecord::base belongs_to :quiz end
now want quizsubmissions' course of study id, i.e.quiz_submission.quiz.part.topic.course.id
each quiz_submission.
my current code quizsubmission.all.map{|qs| qs.quiz.part.topic.cours.id}
insanely slow, have tried joins
, still did not improve performance. maybe used joins
wrongly.
any suggestions? should create quizsubmission belongs_to :course
? thanks!
you can do:
quizsubmission.joins(:quiz => {:part => {:topic => :course}}). select('quiz_submissions.id, courses.id course_id')
ruby-on-rails ruby
No comments:
Post a Comment