Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Note < 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
- class Note < 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
- 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
- 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 <top (required)>'
- 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}) }
- class Note
- belongs_to :volunteer,
- ->(note) {where('1 = ?', (note.subject_type == 'Volunteer')},
- :foreign_key => 'subject_id'
- end
- module Notable
- def self.included(other)
- Note.belongs_to(other.to_s.underscore.to_sym,
- ->(note) {where('1 = ?', note.subject_type == other.to_s)},
- {:foreign_key => :subject_id})
- end
- end
- ->(note) {(note.subject_type == "Volunteer") ? where('1 = 1') : none}
- belongs_to :volunteer, :foreign_key => :subject_id,
- :conditions => Proc.new {['1 = ?', (subject_type == 'Volunteer')]}
- class Note < ActiveRecord::Base
- # The true polymorphic association
- belongs_to :subject, polymorphic: true
- # The trick to solve this problem
- has_one :self_ref, :class_name => self, :foreign_key => :id
- has_one :volunteer, :through => :self_ref, :source => :subject, :source_type => Volunteer
- has_one :participation, :through => :self_ref, :source => :subject, :source_type => Participation
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement