Help with relationship

Let’s imagine we are modelling a company that have several shops. I
have a Worker model and a Shop model. A worker belongs_to a shop, and
each shop has_many workers. But, in a shop there’s a distinguished
worker that is the supervisor

 create table shops (
     ...
     worker_id integer -- the supervisor
 );

How would you express this relationship?

On Tue, 2006-02-07 at 22:59 +0100, Xavier N. wrote:

How would you express this relationship?


seems to me you have distinct tables there…

shops
employees

if your model of shops (i.e. a single shop record) might have more than
one worker/supervisor/employee then it is another table. If there can
only be one supervisor, he might be in the shop record…otherwise, you
would have another column in your ‘workers’ table that adds a ‘role’
function (i.e. supervisor).

Craig

Hi Xavier,

I think you would to create other table with the worker’s types

So

class WorkerType < AvtiveRecord::Base
has_many :worker
end

AND

class Worker < ActiveRecord::Base
belongs_to :worker_type
end

PD: Sorry for my english :stuck_out_tongue:

Saludos,

Jean Carlo Schechnner
IDENTIDAD VIRTUAL
www.IDVIRTUAL.com
skype: identidadvirtual

On Feb 7, 2006, at 23:19, Craig W. wrote:

only be one supervisor, he might be in the shop record…otherwise,
you
would have another column in your ‘workers’ table that adds a ‘role’
function (i.e. supervisor).

Oh sorry, I wanted to be brief but that didn’t work :-).

Yes, I have tables:

 create table workers (
   ...
   is_supervisor boolean,
   center_id     integer
 );

 create table centers (
   ...
   worker_id integer -- the supervisor
 );

and corresponding models:

 class Worker < ActiveRecord::Base
   belongs_to :center
 end

 class Center < ActiveRecord::Base
   has_many :workers
 end

Now that lacks the supervisor relationship (let’s suppose we cannot
assume a supervisor works in the center it supervises, otherwise we
could just grab the center_id from workers).

By imitation I would write

 class Center < ActiveRecord::Base
   has_many :workers
   belongs_to :worker
 end

but that feels strange and I don’t control whether is actually right.

– fxn