# Find query issue

**URL:** <https://www.ruby-forum.com/t/find-query-issue/87787>\
**Category:** Rails\
**Created:** [March 6, 2007, 12:24am UTC](https://www.ruby-forum.com/t/find-query-issue/87787 "2007-03-06T00:24:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [March 6, 2007, 12:24am UTC](https://www.ruby-forum.com/t/find-query-issue/87787/1 "2007-03-06T00:24:16Z")

</div>

Hey all  
I have:

Item has\_many :refs  
Ref belongs\_to :item  
Item has\_many :sales  
Sale belongs\_to :item

I would like to find all refs that have been sold 2 days ago and which  
name=‘gizmo’

I tried that:  
@refs= Ref.find(:all,:include=\>[‘item’,‘sale’]  
:conditions=\>[refs.name=? and  
items.sales.created\_on=?’,’%gizmo%,2.days.ago’])

but item.sales.created\_on. any idea how to make the query right?

thanx in advance

Pat

---

<div class="post-metadata">

**Author:** ![patcito](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/50afbb/32.png) [@patcito](https://www.ruby-forum.com/u/patcito)\
**Post date:** [March 6, 2007, 8:16am UTC](https://www.ruby-forum.com/t/find-query-issue/87787/2 "2007-03-06T08:16:55Z")

</div>

Patrick A. wrote:

> :conditions=\>[refs.name=? and  
> items.sales.created\_on=?‘,’%gizmo%,2.days.ago’])
> 
> but item.sales.created\_on. any idea how to make the query right?

Have a read of the Table Aliasing Section of the  
ActiveRecord::Associations  
API: [http://pages.citebite.com/r1s2x4d5s2mgd](http://pages.citebite.com/r1s2x4d5s2mgd)

Look at SQL the shown in the development log or error page to help you  
find the right table name to use.

–  
We develop, watch us RoR, in numbers too big to ignore.
