ruby on rails - Active Record association has_many with multiple foreign keys -


i'm trying set referral model. referral contains user referred, user referring, , doctor

class user < activerecord::base   has_many :referrals   belongs_to :profile, polymorphic: true end  class referral < activerecord::base   belongs_to :user   belongs_to :referrer, :class_name => "user"   belongs_to :doctor, :class_name => "user" end 

i'm able create generic has_many :referrals see doctors have been referred user i'd see doctors you've referred others (using referrer column).

i've tried has_many :doctors_referred, primary_key: "referrer_id" , has_many :doctors_referred, through: :referrals ,source: "referrer" no luck. how can see doctors user has referred?

# irrelevant  class patientprofile < activerecord::base   has_one :user, as: :profile end  class doctorprofile < activerecord::base   has_one :user, as: :profile end 

my first attempt this:

class user < activerecord::base   has_many :referrals_as_referrer, source: :referred   has_many :referrals_as_referree, source: :user    has_many :doctors_as_referrer, through: :referrals_as_referrer   has_many :doctors_as_referree, through: :referrals_as_referree end 

the issue here saying has_many :referrals isn't enough because can have referral referrer or 1 referree. can type user.referrals_as_referrer give list of referrals user referrer. can write user.doctors_as_referrer go through previous association , retrieve list of users represent doctors referred referral in user.referrals_as_referrer list.

i haven't tested , might wrong, let's iterate once try out.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -