Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 20th, 2012  |  syntax: None  |  size: 0.79 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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)