# Analogue to has\_many :through

**URL:** <https://www.ruby-forum.com/t/analogue-to-has-many-through/145374>\
**Category:** Rails\
**Created:** [August 8, 2008, 3:16pm UTC](https://www.ruby-forum.com/t/analogue-to-has-many-through/145374 "2008-08-08T15:16:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrius\_c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19adc/32.png) [@andrius\_c](https://www.ruby-forum.com/u/andrius_c)\
**Post date:** [August 8, 2008, 3:16pm UTC](https://www.ruby-forum.com/t/analogue-to-has-many-through/145374/1 "2008-08-08T15:16:04Z")

</div>

Let’s say i have three models: user, message, picture. User has many  
messages and pictures, picture belongs to user, message belongs to  
user. Now if i want to get pictures of author of specific message i  
call message.user.pictures. However this means i get two queries to  
database ‘select \* from users where id = [message.user\_id] limit 1’  
and ‘select \* from pictures where user\_id = [user.id]’. But is there  
any way to force only one query ‘select \* from pictures where user\_id  
= [message.user\_id]’ except for pictures = Picture.all(:conditions =\>  
{:user\_id =\> message.user\_id}) ?

---

<div class="post-metadata">

**Author:** ![andrius\_c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19adc/32.png) [@andrius\_c](https://www.ruby-forum.com/u/andrius_c)\
**Post date:** [August 8, 2008, 3:46pm UTC](https://www.ruby-forum.com/t/analogue-to-has-many-through/145374/2 "2008-08-08T15:46:33Z")

</div>

how about:  
Message.user.find(:all, :include =\> :pictures)  
i might be wrong

---

<div class="post-metadata">

**Author:** ![andrius\_c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/e19adc/32.png) [@andrius\_c](https://www.ruby-forum.com/u/andrius_c)\
**Post date:** [August 8, 2008, 7:25pm UTC](https://www.ruby-forum.com/t/analogue-to-has-many-through/145374/3 "2008-08-08T19:25:40Z")

</div>

Message has only one user. Tried message.user(:include =\> :pictures)  
but it didn’t work.
