has_many relation doesn't seems right or logical in business perceptive, needed some thing like belo

Posted by Vijendra on Stack Overflow See other posts from Stack Overflow or by Vijendra
Published on 2010-04-24T08:13:35Z Indexed on 2010/04/24 8:43 UTC
Read the original article Hit count: 200

My situation is like this. Company has many users and users may belongs to many companies. And current implementation is something like below.


  class Company 
    has_many :employments
    has_many :users, :through => :employments
  end

  class Employment 
    belongs_to :company
    belongs_to :user
  end

  class User
    has_many :employments
    has_many :companies, :through => :employments  #This doesn't looks correct
  end

It works, but "user has many companies" doesn't looks logically meaningful. It must be some thing like belongs_to_many companies. Do I need to use has_and_belongs_to_many?

Can some one please suggest the right way for representing these relationships?

© Stack Overflow or respective owner

Related posts about rails

Related posts about has-many