Forum: Ruby on Rails Crazy @55 Inheritance

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Ff82af3238a57fbd1212832ec1a19f28?d=identicon&s=25 Dylan Stamat (Guest)
on 2006-02-10 00:56
(Received via mailing list)
I have one table, called "people".
Within this table, there are type "users", "managers".... etc.  So,

Now, I want a "user" to have one manager, but a "manager" to have many

Since I don't want to use HABTM... how do I set this relationship up
that :has_one, :belongs_to and :has_many don't
allow you to specify a :join_table ?

I can get the "has_one" part of the equation working using:
belongs_to :manager,
                 :class_name  => "Client",
                 :foreign_key => "assigned_manager"

... where "assigned_manager" is just a field with the "people" table...
I'm at a loss on how to specify the :has_many.

My assumption would be that I have to use HABTM in order to get the join
table recognized... but just need to ensure I'm not assigning
many managers to one user.  Is this correct ?
Any clarification on this would be great.
Thank you !
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-02-10 10:07
(Received via mailing list)
Dylan Stamat wrote:
> many managers to one user.  Is this correct ?
> Any clarification on this would be great.
Why do you need a join table?  Sounds like a standard has_many to me.
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (Guest)
on 2006-02-10 13:12
(Received via mailing list)
this is actually a mix of STI and a self referential join.

type ('Employee' or 'Manager')
manager_id (references field)

class Person < ActiveRecord::Base

class Employee < Person
  belongs_to :manager

class Manager < Person
  has_many :employees

bob = Manager.create(:name => "Bob") # create bob as a manager
jack = Employee.create(:name => "Jack") # create jack as an employee
bob.employees << jack # assign jack as an employee of bob, this sets the
manager_id field in the 'jack' record to the id of the 'bob' record

adjust to fit your specific setup
This topic is locked and can not be replied to.