Note that there are some explanatory texts on larger screens.

plurals
  1. PORails has_many through polymorphic source_type scope_chain bug
    text
    copied!<p>Ok, some weird stuff has been happening since I set up some polymorphic :through relationships.</p> <p>I'm using Rails 3.2.12 with Ruby 1.9.3.</p> <p>The relationships are as follows:</p> <pre><code>class User &lt; ActiveRecord::Base has_many :registrations has_many :student_learning_component_statuses, :through =&gt; :registrations has_many :programs, :through =&gt; :student_learning_component_statuses, :source =&gt; :statusable, :source_type =&gt; 'Program' has_many :phases, :through =&gt; :student_learning_component_statuses, :source =&gt; :statusable, :source_type =&gt; 'Phase' </code></pre> <p>Now when I query either a user's programs or phases, whichever I query <strong>first</strong> always succeeds, and the relationship queried <strong>second</strong> returns an empty array.</p> <p>Console usage results shown below:</p> <pre><code>1.9.3p392 :005 &gt; User.find_by_email('s1@nyfs.com').programs User Load (1.4ms) SELECT "users".* FROM "users" WHERE "users"."email" = 's1@nyfs.com' LIMIT 1 Program Load (1.4ms) SELECT "programs".* FROM "programs" INNER JOIN "student_learning_component_statuses" ON "programs"."id" = "student_learning_component_statuses"."statusable_id" INNER JOIN "registrations" ON "student_learning_component_statuses"."registration_id" = "registrations"."id" WHERE "registrations"."user_id" = 137 AND ("student_learning_component_statuses"."statusable_type" = 'Program') =&gt; [#&lt;Program id: 1, title: "Beginner Certification", description: "", school_id: 47, created_at: "2013-06-27 19:46:44", updated_at: "2013-06-27 22:13:27", duration: "16 weeks"&gt;, #&lt;Program id: 2, title: "Instructor Certification", description: "Another Program to test more stuff", school_id: 47, created_at: "2013-06-28 02:14:43", updated_at: "2013-06-28 02:14:43", duration: "20 weeks"&gt;] 1.9.3p392 :006 &gt; User.find_by_email('s1@nyfs.com').phases User Load (0.9ms) SELECT "users".* FROM "users" WHERE "users"."email" = 's1@nyfs.com' LIMIT 1 Phase Load (0.7ms) SELECT "phases".* FROM "phases" INNER JOIN "student_learning_component_statuses" ON "phases"."id" = "student_learning_component_statuses"."statusable_id" INNER JOIN "registrations" ON "student_learning_component_statuses"."registration_id" = "registrations"."id" WHERE "registrations"."user_id" = 137 AND ("student_learning_component_statuses"."statusable_type" = 'Program' AND "student_learning_component_statuses"."statusable_type" = 'Phase') =&gt; [] </code></pre> <p>Note the end of the second query checks that <code>statusable_type = 'Program' AND statusable_type = 'Phase'</code>, which will never be the case. Has anyone encountered an issue like this? Is it time to upgrade to Rails 4? Or maybe something is wrong with my relationships.</p> <p>Please let me know if more code might be useful, and thanks in advance for any suggestions.</p> <h3>[Update]</h3> <p>Based on my research, the fix to this issue has still not been integrated as of the Rails 4 master branch, so I will continue deferring the upgrade to Rails 4 for now. </p>
 

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