title: Association for polymorphic belongs_to of a particular type
body: <p>I'm relatively new to Rails. I would like to add an association to a model that uses the polymorphic association, but returns only models of a particular type, e.g.:</p>
  
  <pre><code>class Note &lt; ActiveRecord::Base
    # The true polymorphic association
    belongs_to :subject, polymorphic: true
  
    # Same as subject but where subject_type is 'Volunteer'
    belongs_to :volunteer, source_association: :subject
    # Same as subject but where subject_type is 'Participation'
    belongs_to :participation, source_association: :subject
  end
  </code></pre>
  
  <p>I've tried a vast array of combinations from reading about the associations on ApiDock but nothing seems to do exactly what I want. Here's the best I have so far:</p>
  
  <pre><code>class Note &lt; ActiveRecord::Base
    belongs_to :subject, polymorphic: true
    belongs_to :volunteer, class_name: "Volunteer", foreign_key: :subject_id, conditions: {notes: {subject_type: "Volunteer"}}
    belongs_to :participation, class_name: "Participation", foreign_key: :subject_id, conditions: {notes: {subject_type: "Participation"}}
  end
  </code></pre>
  
  <p>And I want it to pass this test:</p>
  
  <pre><code>describe Note do
    context 'on volunteer' do
      let!(:volunteer) { create(:volunteer) }
      let!(:note) { create(:note, subject: volunteer) }
      let!(:unrelated_note) { create(:note) }
  
      it 'narrows note scope to volunteer' do
        scoped = Note.scoped
        scoped = scoped.joins(:volunteer).where(volunteers: {id: volunteer.id})
        expect(scoped.count).to be 1
        expect(scoped.first.id).to eq note.id
      end
  
      it 'allows access to the volunteer' do
        expect(note.volunteer).to eq volunteer
      end
  
      it 'does not return participation' do
        expect(note.participation).to be_nil
      end
  
    end
  end
  </code></pre>
  
  <p>The first test passes, but you can't call the relation directly:</p>
  
  <pre><code>  1) Note on volunteer allows access to the volunteer
       Failure/Error: expect(note.reload.volunteer).to eq volunteer
       ActiveRecord::StatementInvalid:
         PG::Error: ERROR:  missing FROM-clause entry for table "notes"
         LINE 1: ...."deleted" = 'f' AND "volunteers"."id" = 7798 AND "notes"."s...
                                                                      ^
         : SELECT  "volunteers".* FROM "volunteers"  WHERE "volunteers"."deleted" = 'f' AND "volunteers"."id" = 7798 AND "notes"."subject_type" = 'Volunteer' LIMIT 1
       # ./spec/models/note_spec.rb:10:in `block (3 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <hr>
  
  <h3>Why?</h3>
  
  <p>The reason I want to do it this way is because I'm constructing a scope based on parsing a query string including joining to various models/etc; the code used to construct the scope is considerably more complex than that above - it uses <code>collection.reflections</code>, etc. My current solution works for this, but it offends me I can't call the relations directly from an instance of Note.</p>
  
  <p>I could solve it by splitting it into two issues: using scopes directly</p>
  
  <pre><code>  scope :scoped_by_volunteer_id, lambda { |volunteer_id| where({subject_type: 'Volunteer', subject_id: volunteer_id}) }
    scope :scoped_by_participation_id, lambda { |participation_id| where({subject_type: 'Participation', subject_id: participation_id}) }
  </code></pre>
  
  <p>and then just using a getter for <code>note.volunteer</code>/<code>note.participation</code> that just returns <code>note.subject</code> if it has the right <code>subject_type</code> (nil otherwise) but I figured in Rails there must be a better way?</p>