Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to handle 4 way relation between 2 models?

I have two models: Company and User

This is the situation:

  • Company can follow another company
  • User can follow a company
  • User can follow another user

What is the best way to define the relationships and how will the join model look like?

Also, are there any best practises when addressing such situations?

Update

Sorry, to have not mentioned this earlier. I am aware of the various relationship types available. My question is 'which is the best fit'?

like image 945
ZX12R Avatar asked Jan 20 '26 07:01

ZX12R


1 Answers

Regarding your question I would suggest you to go through couple of Railscasts videos:

  1. http://railscasts.com/episodes/47-two-many-to-many
  2. http://railscasts.com/episodes/154-polymorphic-association

And this is described very well on RubyonRails website

  • http://guides.rubyonrails.org/association_basics.html

I would say look these for your case:

  1. http://guides.rubyonrails.org/association_basics.html#the-has_many-through-association
  2. http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association

I hope this will help you.

like image 168
Manish Das Avatar answered Jan 23 '26 20:01

Manish Das