Note that there are some explanatory texts on larger screens.

plurals
  1. PORails or SQL to get through a few model associations
    primarykey
    data
    text
    <p>I'm a bit of a n00b when it comes to hacking my own SQL for my ruby on rails app, so your help would be much appreciated. I have a model as follows:</p> <pre><code>Timetable has_many :enrollments Enrollment belongs_to :timetable belongs_to :stream has_one :course, :through =&gt; stream #Stream &amp; Timetable together are unique for an Enrollment Stream has_many :enrollments belongs_to :course Course has_many :streams </code></pre> <p>i.e</p> <p><code>Timetable 1-* Enrollments *-1 Stream *-1 Course</code></p> <p>I'm trying to find the best way to get a set of courses associated with a timetable, through it's enrollments, through each enrollment's streams, which have a course.</p> <p>Preferably something like:</p> <pre><code>Timetable.courses </code></pre> <p>Trying the following had no luck</p> <pre><code>Timetable has_many :courses, :through =&gt; :enrollments, :source =&gt; :course </code></pre> <p>It gave me an error <code>ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source reflection macro :has_one :through for has_many :courses, :through =&gt; :enrollments. Use :source to specify the source reflection.</code></p> <p>I took a look at <a href="https://stackoverflow.com/questions/1926536/invalid-source-reflection-macro-has-many-through">Invalid source reflection macro :has_many :through</a> and it seemed to be pretty close.</p> <p>I also found <a href="http://code.alexreisner.com/articles/has-many-through-habtm.html" rel="nofollow noreferrer">http://code.alexreisner.com/articles/has-many-through-habtm.html</a> but I can't quite grasp how to apply this here (although it is 12am NZT, so maybe that's why!).</p> <p>Help, hints or even the answer would be great!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload