# Eager loading association with condition

**URL:** <https://www.ruby-forum.com/t/eager-loading-association-with-condition/139790>\
**Category:** Rails\
**Created:** [June 7, 2008, 12:37am UTC](https://www.ruby-forum.com/t/eager-loading-association-with-condition/139790 "2008-06-07T00:37:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vikas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac91a4/32.png) [@vikas](https://www.ruby-forum.com/u/vikas)\
**Post date:** [June 7, 2008, 12:37am UTC](https://www.ruby-forum.com/t/eager-loading-association-with-condition/139790/1 "2008-06-07T00:37:43Z")

</div>

Hi,

To explain my problem, I’ll use the standard Post/author example.  
Suppose I have

class Post \< ActiveRecord::Base  
belongs\_to :author  
end

I am using following to fetch the posts and author info in a single  
query -

for post in Post.find(:all, :include =\> :author)  
…

Now, I want to get _only_ the posts whose author’s name starts with  
“A”. How do I do that?

(i.e. sort of Post.find(:all, :include =\> :author, :condition =\>  
["post.author.name like ‘A%’])

Thanks.

---

<div class="post-metadata">

**Author:** ![vikas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ac91a4/32.png) [@vikas](https://www.ruby-forum.com/u/vikas)\
**Post date:** [June 7, 2008, 11:12am UTC](https://www.ruby-forum.com/t/eager-loading-association-with-condition/139790/2 "2008-06-07T11:12:43Z")

</div>

On Jun 6, 6:35 pm, Vikas [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> (i.e. sort of Post.find(:all, :include =\> :author, :condition =\>  
> ["post.author.name like ‘A%’])

Bear in mind that your conditions are just a chunk of sql, so  
post.author.name is meaningless.  
authors.name does mean something to the db and should get you there.

Fred
