Forum: Ruby on Rails ActiveRecord question

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.
9cc7ac65e0c0ab866732c861c6424a97?d=identicon&s=25 Enrico Brunetta (enrico)
on 2009-04-10 17:57
Hello there, newbie speaking

Let's say I have:

class Office
 has_many :holidays #(just a list of dates when the office is closed)
 has_many :doctors #doctors who work in the office
end

class Doctor
 belongs_to :office
 has_many :holidays #(additional dates when this doctor is not
available)
end

class Holiday  (has a office_id, doctor_id and a date field)
end

From the app standpoint I would like to be able to allow the
administrator to set office-wide closed dates, but to allow an
individual doctor to augment that list with additional dates he/she
won't be available in.

What is the best/most elegant way to retrieve the merged list of
holidays for a doctor?

I know I could retrieve the Office holidays in the doctor model and
merge it with the Doctor's holidays, but I was wondering if there was a
more activerecord/rails-specific pattern that I might be missing to
allow one DB query with the 'inherited' holidays from the Office....

Thanks

Enrico.
1e44d920287c3e26c3d7080769b5ccb5?d=identicon&s=25 Jaryl Sim (Guest)
on 2009-04-10 20:50
(Received via mailing list)
I don't know if this might work, but you could give it a shot.

class Doctor
 belongs_to :office
 has_many :holidays, :through => Office, :conditions => ['doctor_id =
NULL OR doctor_id = ?',self.id]
end

There's a very good chance this might not work, so perhaps instead of
an association, maybe you could use a named_scope instead.

On Apr 10, 11:57 pm, Enrico Brunetta <rails-mailing-l...@andreas-
This topic is locked and can not be replied to.