Forum: Ruby on Rails help with relationship

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.
Xavier N. (Guest)
on 2006-02-08 00:01
(Received via mailing list)
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?
Craig W. (Guest)
on 2006-02-08 00:19
(Received via mailing list)
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
Jean Carlo Schechnner (Guest)
on 2006-02-08 00:23
(Received via mailing list)
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 :P

Saludos,

Jean Carlo Schechnner
IDENTIDAD VIRTUAL
www.IDVIRTUAL.com
skype: identidadvirtual
Xavier N. (Guest)
on 2006-02-08 00:35
(Received via mailing list)
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
This topic is locked and can not be replied to.