# User hierarchy

**URL:** <https://www.ruby-forum.com/t/user-hierarchy/175415>\
**Category:** Rails\
**Created:** [September 13, 2009, 12:10pm UTC](https://www.ruby-forum.com/t/user-hierarchy/175415 "2009-09-13T12:10:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpflo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71e660/32.png) [@rpflo](https://www.ruby-forum.com/u/rpflo)\
**Post date:** [September 13, 2009, 12:10pm UTC](https://www.ruby-forum.com/t/user-hierarchy/175415/1 "2009-09-13T12:10:01Z")

</div>

I’m trying to figure out how to have a two level user relationship.

Photographers have clients. Clients have one photographer. Both are  
Users.

# So I’ve got a User model that looks like this:

class User \< ActiveRecord::Base

has\_many :client\_associations, :foreign\_key =\>  
‘client\_id’, :class\_name =\> ‘Association’, :dependent =\> :destroy  
has\_many :clients, :through =\> :client\_associations

has\_many :photographer\_associations, :foreign\_key =\>  
‘photographer\_id’, :class\_name =\> ‘Association’, :dependent  
=\> :destroy  
has\_one :photographer, :through =\> :photographer\_associations

def self.find\_by\_login\_or\_email(login)  
User.find\_by\_login(login) || User.find\_by\_email(login)  
end

# end

# And an Association model that looks like:

class Association \< ActiveRecord::Base  
belongs\_to :client, :class\_name =\> ‘User’  
belongs\_to :photographer, :class\_name =\> ‘User’  
end

When I fill it with some data and fire up the console, running  
user.clients.all or user.photographer just gives me an empty array.

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![rpflo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/71e660/32.png) [@rpflo](https://www.ruby-forum.com/u/rpflo)\
**Post date:** [September 13, 2009, 4:05pm UTC](https://www.ruby-forum.com/t/user-hierarchy/175415/2 "2009-09-13T16:05:53Z")

</div>

Just had to swap the foreign keys

has\_many :client\_associations,  
:foreign\_key =\> ‘photographer\_id’,  
:class\_name =\> ‘Association’,  
:dependent =\> :destroy

has\_many :clients, :through =\> :client\_associations

has\_one :photographer\_association,  
:foreign\_key =\> ‘client\_id’,  
:class\_name =\> ‘Association’,  
:dependent =\> :destroy

has\_one :photographer, :through =\> :photographer\_association
