# Re: Active record question

**URL:** <https://www.ruby-forum.com/t/re-active-record-question/147927>\
**Category:** Rails\
**Created:** [September 7, 2008, 4:29am UTC](https://www.ruby-forum.com/t/re-active-record-question/147927 "2008-09-07T04:29:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [September 7, 2008, 4:29am UTC](https://www.ruby-forum.com/t/re-active-record-question/147927/1 "2008-09-07T04:29:12Z")

</div>

Thanks Phlip -

Thanks for the response - I’ll play around with your suggestions.

What I was trying to do was something like this project.appointments to  
display all the appointments for a specific project.

The way I have it set up is that a project has many tasks, each task can  
have many resources (people) and each resource then has appointments.  
I’m not sure this is the best way to model but I thought it would be  
easier just keeping one reference to the project\_id in the tasks table  
rather then trying to manage all the links back up to the projects table  
from the resources and appointments tables. Maybe a bad design  
decision??

----- Original Message ----  
From: Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
To: removed\_email\_address@domain.invalid  
Sent: Saturday, September 6, 2008 8:52:49 PM  
Subject: [Rails] Re: Active record question

Mark S. wrote:

> Where project\_id = 1;  
> Why is project joined in, if project\_id comes from tasks?

And why is resources joined in? for its data? It does not participate in  
the  
links, right?

> I can get all the Resources for a project using this but can not get the  
> appointments. I could loop through the resources getting all the  
> appointmen ts and tracking them, but that doesn’t seem like the right  
> way to do it. I could also use find\_by\_sql, but again, that does’t seem  
> right.
> 
> class Project \< ActiveRecord::Base  
> has\_many :tasks  
> belongs\_to :customer  
> has\_many :resources, :through =\> :tasks

Appointment.find( :all, :include =\> :tasks,  
:conditions =\> { :‘tasks.project\_id’ =\> 1} )

In summary, provide enough :has\_many =\> :through to get to the keying  
table,  
then put it into your :include and :conditions.

Warning: :include will eager-load, which might slow you down with  
irrelevant data.

Alternately…

Task.find\_all\_by\_project\_id(1).map(&:appointments).uniq

(-:

–  
Phlip

---

<div class="post-metadata">

**Author:** ![mslagell](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@mslagell](https://www.ruby-forum.com/u/mslagell)\
**Post date:** [September 7, 2008, 6:12am UTC](https://www.ruby-forum.com/t/re-active-record-question/147927/2 "2008-09-07T06:12:40Z")

</div>

Mark S. wrote:

> What I was trying to do was something like this project.appointments to  
> display all the appointments for a specific project.

Briefly, since you seem to have more experience with raw SQL than  
ActiveRecord  
(better than the other way around!;), try inspect\_sql to see what you  
are  
getting as you tune this.

But I should let someone else take a crack at the data model question  
now.

–  
Phlip
