Skip to content

Instantly share code, notes, and snippets.

@zosiu
Created January 21, 2015 14:03
Show Gist options
  • Save zosiu/c36decbb78aeba167b17 to your computer and use it in GitHub Desktop.
Save zosiu/c36decbb78aeba167b17 to your computer and use it in GitHub Desktop.
Association for polymorphic belongs_to of a particular type
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment