Advertisement
Guest User

Untitled

a guest
Sep 21st, 2014
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. class Note < ActiveRecord::Base
  2. # The true polymorphic association
  3. belongs_to :subject, polymorphic: true
  4.  
  5. # Same as subject but where subject_type is 'Volunteer'
  6. belongs_to :volunteer, source_association: :subject
  7. # Same as subject but where subject_type is 'Participation'
  8. belongs_to :participation, source_association: :subject
  9. end
  10.  
  11. class Note < ActiveRecord::Base
  12. belongs_to :subject, polymorphic: true
  13. belongs_to :volunteer, class_name: "Volunteer", foreign_key: :subject_id, conditions: {notes: {subject_type: "Volunteer"}}
  14. belongs_to :participation, class_name: "Participation", foreign_key: :subject_id, conditions: {notes: {subject_type: "Participation"}}
  15. end
  16.  
  17. describe Note do
  18. context 'on volunteer' do
  19. let!(:volunteer) { create(:volunteer) }
  20. let!(:note) { create(:note, subject: volunteer) }
  21. let!(:unrelated_note) { create(:note) }
  22.  
  23. it 'narrows note scope to volunteer' do
  24. scoped = Note.scoped
  25. scoped = scoped.joins(:volunteer).where(volunteers: {id: volunteer.id})
  26. expect(scoped.count).to be 1
  27. expect(scoped.first.id).to eq note.id
  28. end
  29.  
  30. it 'allows access to the volunteer' do
  31. expect(note.volunteer).to eq volunteer
  32. end
  33.  
  34. it 'does not return participation' do
  35. expect(note.participation).to be_nil
  36. end
  37.  
  38. end
  39. end
  40.  
  41. 1) Note on volunteer allows access to the volunteer
  42. Failure/Error: expect(note.reload.volunteer).to eq volunteer
  43. ActiveRecord::StatementInvalid:
  44. PG::Error: ERROR: missing FROM-clause entry for table "notes"
  45. LINE 1: ...."deleted" = 'f' AND "volunteers"."id" = 7798 AND "notes"."s...
  46. ^
  47. : SELECT "volunteers".* FROM "volunteers" WHERE "volunteers"."deleted" = 'f' AND "volunteers"."id" = 7798 AND "notes"."subject_type" = 'Volunteer' LIMIT 1
  48. # ./spec/models/note_spec.rb:10:in `block (3 levels) in <top (required)>'
  49.  
  50. scope :scoped_by_volunteer_id, lambda { |volunteer_id| where({subject_type: 'Volunteer', subject_id: volunteer_id}) }
  51. scope :scoped_by_participation_id, lambda { |participation_id| where({subject_type: 'Participation', subject_id: participation_id}) }
  52.  
  53. class Note
  54. belongs_to :volunteer,
  55. ->(note) {where('1 = ?', (note.subject_type == 'Volunteer')},
  56. :foreign_key => 'subject_id'
  57. end
  58.  
  59. module Notable
  60. def self.included(other)
  61. Note.belongs_to(other.to_s.underscore.to_sym,
  62. ->(note) {where('1 = ?', note.subject_type == other.to_s)},
  63. {:foreign_key => :subject_id})
  64. end
  65. end
  66.  
  67. ->(note) {(note.subject_type == "Volunteer") ? where('1 = 1') : none}
  68.  
  69. belongs_to :volunteer, :foreign_key => :subject_id,
  70. :conditions => Proc.new {['1 = ?', (subject_type == 'Volunteer')]}
  71.  
  72. class Note < ActiveRecord::Base
  73. # The true polymorphic association
  74. belongs_to :subject, polymorphic: true
  75.  
  76. # The trick to solve this problem
  77. has_one :self_ref, :class_name => self, :foreign_key => :id
  78.  
  79. has_one :volunteer, :through => :self_ref, :source => :subject, :source_type => Volunteer
  80. has_one :participation, :through => :self_ref, :source => :subject, :source_type => Participation
  81. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement