# ActiveRecord question

**URL:** <https://www.ruby-forum.com/t/activerecord-question/165246>\
**Category:** Rails\
**Created:** [April 10, 2009, 5:57pm UTC](https://www.ruby-forum.com/t/activerecord-question/165246 "2009-04-10T17:57:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![enrico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e495f1/32.png) [@enrico](https://www.ruby-forum.com/u/enrico)\
**Post date:** [April 10, 2009, 5:57pm UTC](https://www.ruby-forum.com/t/activerecord-question/165246/1 "2009-04-10T17:57:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![enrico](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/e495f1/32.png) [@enrico](https://www.ruby-forum.com/u/enrico)\
**Post date:** [April 10, 2009, 8:50pm UTC](https://www.ruby-forum.com/t/activerecord-question/165246/2 "2009-04-10T20:50:24Z")

</div>

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-
