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
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).
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 :-).
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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.