Note that there are some explanatory texts on larger screens.

plurals
  1. POModel traversal in rails: from child to sibling's child
    primarykey
    data
    text
    <p>I have the following model:</p> <pre><code>class Advisor &lt; ActiveRecord::Base belongs_to :course end class Course &lt; ActiveRecord::Base has_many :advisors has_many :sessions has_many :materials, :through=&gt;:sessions end class Session &lt; ActiveRecord::Base belongs_to :course has_many :materials end class Material &lt; ActiveRecord::Base belongs_to :session end </code></pre> <p>I.e., every advisor teaches one course, every course has sessions, and every session has materials. I want to traverse from an advisor to all the associated materials, i.e. something like: <code>Advisor.first.materials</code> I tried to do:</p> <pre><code>class Advisor &lt; ActiveRecord::Base belongs_to :course has_many :sessions, :through=&gt;:course has_many :materials, :through=&gt;:sessions end </code></pre> <p>But it didn't work as it treated sessions as a many-to-many table: <code>Unknown column 'sessions.advisor_id' in 'where clause': SELECT 'material'.* FROM 'materials' INNER JOIN 'sessions' ON 'materials'.session_id = 'sessions'.id WHERE (('sessions'.advisor_id = 1))</code></p> <p>I then tried to do:</p> <pre><code>class Advisor &lt; ActiveRecord::Base belongs_to :course has_many :materials, :through=&gt;:course end </code></pre> <p>In an attempt to have the association use the "materials" association in the "Course" model, but received: </p> <pre><code>ActiveRecord::HasManyThroughSourceAssociationMacroError: Invalid source reflection on macro :has_many :through for has_many :materials, :through=&gt;:sessions. Use :source to specify the source reflection. </code></pre> <p>Tried to use "sessions" as the source which was a nice try but made me receive only the sessions rather than the materials.</p> <p>Any ideas if this is at all possible? I'm using Rails 2.3.8 (perhaps time to upgrade?)</p> <p>Thanks! Amit</p>
    singulars
    1. This table or related slice is empty.
    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.
    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