User hierarchy

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?

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs