ruby on rails - has_and_belongs_to_many association that acts as "through" -


models

patient:

description:
should have 1 referrer

association:

has_and_belongs_to_many :referrer 


referrer:

description:
contains types of referrers such as, not limited to:

  • partners
  • etc.

association:

has_and_belongs_to_many :referrer 


referrer type (a new model think should add):

details of referrers here, such name of website lead patient go hospital, or name of partner suggested hospital patient.




workflow & explanations

in form, hospital employee takes details of patient. 1 of details referrer -- entity referred patient hospital. referrer rendered dropdown listbox.

i made association between patient-referrer habtm because of following reasons:

  1. the referrer model used model aside patient.
  2. the referrer model has own scaffold admin can add/remove referrers.




requirement (the problem)

every time dropdown box's value "partner", there should field input name of entity. 3rd model come in. 3rd model contain the:

  1. name of partner
  2. a reference referrer model's record named "partner"
  3. a reference patient record.

with 3rd model in play, how able insert new referrer details each patient per referrer, , making cascadable, in sense when delete patient, referrer detail deleted, referrer still kept untouched, rails way.

has_and_belongs_to_many not great

has_and_belongs_to_many (habtm) 1 limited relations in activerecord , fits need simple many-to-many relationship and:

  • you never need join model or attach logic join table
  • you never need attach data join table
  • you never need query join table separately

there no habtm "through" because habtm not have join model. thats kind of whole point. cannot use polymorphism in habtm relationship - requires join model.

using habtm not correct choice here.

in cases should using has_many :though instead.

here want 1 many relation join model:

class patient < activerecord::base   has_one :referral, dependent: :destroy   has_one :referrer, through: :referral end  class referral < activerecord::base   belongs_to :patient   belongs_to :partner   validates_uniqueness_of :patient_id end  class partner < activerecord::base   has_many :referrals   has_many :referred_patients, through: :referrals end 

polymorpism

it possible use polymorphism let same associations point different tables:

class referral < activerecord::base   belongs_to :referred, polymorphic: :true   belongs_to :referrer, polymorphic: :true   validates_uniqueness_of :referred_id, scope: :referred_type end 

you need add referred_type:string , referrer_type:string columns referrals.

# app/models/concerns/referable.rb module referable   extend activesupport::concern   included     has_one :referral, as: :referred, dependent: :destroy     has_one :referrer, through: :referral     has_many :referrals, as: :referrer     has_many :referred_entities, through: :referrals                         source: :referred   end end  class patient < activerecord::base   include referable end  class doctor < activerecord::base   include referable end  class partner < activerecord::base   include referable end 

this example let patient, doctor , partner either referring party or have referral. 1 big drawback lack of joins , eager loading polymorphic associations since joined table cannot known beforehand.


Comments

Popular posts from this blog

PySide and Qt Properties: Connecting signals from Python to QML -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

scala - 'wrong top statement declaration' when using slick in IntelliJ -