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
on 2006-02-10 01:56
on 2006-02-10 11:07
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.
on 2006-02-10 14:12
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