ruby on rails - What is the best way to handle 4 way relation between 2 models? -


i have 2 models: company , user

this situation:

  • company can follow company
  • user can follow company
  • user can follow user

what best way define relationships , how join model like?

also, there best practises when addressing such situations?

update

sorry, have not mentioned earlier. aware of various relationship types available. question 'which best fit'?

thanks polymorphic associations, can put relations 1 table this:

create_table :follows |t|         t.references :followable, :polymorphic => true         t.references :followed_by, :polymorphic => true end 

then models are:

class user < activerecord::base     has_many :following_objects, :class_name => 'follow', :as => :followed_by     has_many :followed_objects, :class_name => 'follow', :as => :followable end  class company < activerecord::base     has_many :following_objects, :class_name => 'follow', :as => :followed_by     has_many :followed_objects, :class_name => 'follow', :as => :followable end  class follow < activerecord::base     belongs_to :followable, :polymorphic => true     belongs_to :followed_by, :polymorphic => true end 

sorry ugly names.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -