# Active record question

**URL:** <https://www.ruby-forum.com/t/active-record-question/147920>\
**Category:** Rails\
**Created:** [September 7, 2008, 12:56am UTC](https://www.ruby-forum.com/t/active-record-question/147920 "2008-09-07T00:56:54Z")\
**Posts on this page:** 5\
**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, 12:56am UTC](https://www.ruby-forum.com/t/active-record-question/147920/1 "2008-09-07T00:56:54Z")

</div>

I’m not quite sure how to get at this data using Actrive record… I’m  
trying to get all the appointments made for a particular project. I’m  
using four models, Projects, Tasks, Resources and Appointments.

The SQL gets me what I’m looking for but how do I do it using Active  
Record?

Select a.\* from appointments a  
join resources r on r.id = a.resource\_id  
join tasks t on t.id = r.task\_id  
join projects p on p.id = t.project\_id  
Where project\_id = 1;

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  
appointments 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

Any help would be greatly appreciated.

---

<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, 2:53am UTC](https://www.ruby-forum.com/t/active-record-question/147920/2 "2008-09-07T02:53:15Z")

</div>

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, 12:20pm UTC](https://www.ruby-forum.com/t/active-record-question/147920/3 "2008-09-07T12:20:39Z")

</div>

On Sep 7, 1:52 am, Phlip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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.

In a situation like this :joins might be more appropriate - it will  
write the joins but won’t do any of the stuff :include would

Fred

---

<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, 1:55pm UTC](https://www.ruby-forum.com/t/active-record-question/147920/4 "2008-09-07T13:55:28Z")

</div>

> In a situation like this :joins might be more appropriate - it will  
> write the joins but won’t do any of the stuff :include would

Can you :joins =\> :leaf\_records ? Or do you got to write all the raw  
SQL?

---

<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, 2:30pm UTC](https://www.ruby-forum.com/t/active-record-question/147920/5 "2008-09-07T14:30:38Z")

</div>

On 7 Sep 2008, at 12:54, Phlip wrote:

> > In a situation like this :joins might be more appropriate - it will  
> > write the joins but won’t do any of the stuff :include would
> 
> Can you :joins =\> :leaf\_records ? Or do you got to write all the raw  
> SQL?

You can either pass raw sql or options in exactly the same format as  
include takes them (eg :joins =\> [{:foo =\> {:bar =\>  
[:baz, :bam]}}, :bim])

Fred
