Forum: Ruby on Rails Crazy @55 Inheritance

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Dylan S. (Guest)
on 2006-02-10 01:56
(Received via mailing list)
I have one table, called "people".
Within this table, there are type "users", "managers".... etc.  So,
basic
STI.

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

Since I don't want to use HABTM... how do I set this relationship up
seeing
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...
but
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 !
==
Dylan
Alex Y. (Guest)
on 2006-02-10 11:07
(Received via mailing list)
Dylan S. 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.
Chris H. (Guest)
on 2006-02-10 14:12
(Received via mailing list)
this is actually a mix of STI and a self referential join.

people
----------
id
type ('Employee' or 'Manager')
manager_id (references people.id field)
name
...

models
----------
class Person < ActiveRecord::Base
end

class Employee < Person
  belongs_to :manager
end

class Manager < Person
  has_many :employees
end

script/console
----------
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.