has_many relation doesn't seems right or logical, some thing like belongs_to_many looks right

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:23 UTC
Read the original article Hit count: 226

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

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? But that also will gives the same meaning.

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