Guest User

Untitled

a guest
Jun 20th, 2012
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. Rails or SQL to get through a few model associations
  2. Timetable
  3. has_many :enrollments
  4.  
  5. Enrollment
  6. belongs_to :timetable
  7. belongs_to :stream
  8. has_one :course, :through => stream
  9. #Stream & Timetable together are unique for an Enrollment
  10.  
  11. Stream
  12. has_many :enrollments
  13. belongs_to :course
  14.  
  15. Course
  16. has_many :streams
  17.  
  18. Timetable.courses
  19.  
  20. Timetable
  21. has_many :courses, :through => :enrollments, :source => :course
  22.  
  23. SELECT c.*
  24. FROM courses c
  25. LEFT JOIN streams s ON s.course_id = c.id
  26. LEFT JOIN enrollments e ON e.stream_id = s.id
  27. LEFT JOIN timetables t ON e.timetable_id = t.id
  28. WHERE t.id = 5
  29.  
  30. Course.joins(:streams => {:enrollments => :timetables}).to_sql
  31.  
  32. Course.joins(:streams => {:enrollments => :timetables}).where(some condition on timetables)
Advertisement
Add Comment
Please, Sign In to add comment