Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For question #2, the keys in the where clause should map to table names, not association names. So I think you would want:</p> <pre class="lang-rb prettyprint-override"><code>Question.joins(:topics).where(Topic.table_name =&gt; {:id =&gt; [...]}) </code></pre> <p>For question #1, it appears that when you set <code>question.topics = [...]</code>, the Classification objects which Rails creates are being set with a taxonomy_type of "Taxonomy" (instead of "Topic"). That appears to be due to Rails' through_association.rb:51, which takes the <code>base_class</code> of the model being stored, instead of just the actual class name.</p> <p>I was able to get around this with a before_validation callback on the Classification model. It seems to me that the alternative is a patch to the actual Rails associations code, to make this behavior configurable.</p> <pre class="lang-rb prettyprint-override"><code>class Classification &lt; ActiveRecord::Base attr_accessible :classifiable, :classifiable_id, :classifiable_type, :taxonomy, :taxonomy_id, :taxonomy_type belongs_to :classifiable, polymorphic: true belongs_to :taxonomy, polymorphic: true before_validation :set_valid_types_on_polymorphic_associations protected def set_valid_types_on_polymorphic_associations self.classifiable_type = classifiable.class.model_name if classifiable self.taxonomy_type = taxonomy.class.model_name if taxonomy end end </code></pre> <p><strong>UPDATE</strong></p> <p>There appears to be another Rails decision (in <a href="https://github.com/rails/rails/blob/master/activerecord/lib/active_record/associations/preloader/association.rb#L113" rel="nofollow">preloader/association.rb:113</a>) to use the <code>model.base_class.sti_name</code> instead of the <code>model.sti_name</code> when setting scope for associations.</p> <p>That gem should take care of this for you. See <a href="https://github.com/appfolio/store_base_sti_class/blob/master/lib/store_base_sti_class_for_3_1_and_above.rb#L135" rel="nofollow">store_base_sti_class_for_3_1_and_above.rb:135</a> for how it wraps the <code>has_many :as</code> option. In my local environment, this works as expected:</p> <pre><code>$ bundle exec rails console irb(main):001:0&gt; topics = 3.times.map { Topic.create } irb(main):002:0&gt; p = Professional.new irb(main):003:0&gt; p.topics = topics irb(main):004:0&gt; p.save! irb(main):005:0&gt; exit $ bundle exec rails console irb(main):001:0&gt; puts Professional.find(1).topics.to_sql SELECT "taxonomies".* FROM "taxonomies" INNER JOIN "classifications" ON "taxonomies"."id" = "classifications"."taxonomy_id" WHERE "taxonomies"."type" IN ('Topic') AND "classifications"."classifiable_id" = 2 AND "classifications"."classifiable_type" = 'Professional' AND "classifications"."taxonomy_type" IN ('Topic') irb(main):002:0&gt; Professional.find(1).topics.count =&gt; 3 </code></pre>
 

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