ruby on rails - How to relate User and Client Portfolio models in Active Record? -


i'm writing rails client phone-book company provides audit services. company has client portfolio , these clients divided among employees (managers, seniors , assistants). see following diagram, instance:

                               manager                             40 customers                                   |                    ________________________________                    |                              |                 senior                          senior              20 customers                    20 customers                    |                              |            __________________             _________________            |                |             |               |       assistant        assistant      assistant        assistant     10 customers     10 customers    10 customers     10 customers                  *total customer portfolio: 40 customers 

the application show user list of phone numbers belonging client user has assigned, need associate user model client model, question is: 1 of active record associations provides best solution problem?

after reading association basics described in ruby on rails guides, think use has_many :through third model (assignment), given every client in portfolio needs have manager, senior , assistant assigned (as happens away keyboard).

my solution following:

class user < activerecord::base   has_many :assignments   has_many :clients, through: :assignments end  class assignment < activerecord::base   belongs_to :user   belongs_to :client     end  class client < activerecord::base   has_many :assignments   has_many :users, through: :assignments end 

what think of solution above? or 1 more elegant solution?

create table called relationship (or something) 2 foreign_ids concerning want user , client able 1 (example able "follow" 1 => follower_id , following_id). define methods in models related ids , call methods in views display relationship.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -