# Can active record support: 'find' parent objects, but filter

**URL:** <https://www.ruby-forum.com/t/can-active-record-support-find-parent-objects-but-filter/109600>\
**Category:** Rails\
**Created:** [July 17, 2007, 8:16am UTC](https://www.ruby-forum.com/t/can-active-record-support-find-parent-objects-but-filter/109600 "2007-07-17T08:16:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [July 17, 2007, 8:16am UTC](https://www.ruby-forum.com/t/can-active-record-support-find-parent-objects-but-filter/109600/1 "2007-07-17T08:16:00Z")

</div>

Hi,

Can active record handle the following? Or do I need to stick to  
“.find\_by\_sql” to do this:

select parent.id, parent.title, parent.description from parent, child  
where parent.id = child.parent\_id AND child.memo like  
‘%texttosearch%’

That is, I want to effectively ‘find’ parent objects, but filtered based  
on  
the contents of it’s associated child objects data. So the question is  
if  
it is possible what would the ruby code be to do this? i.e. results =  
Parent.find(:all, …

Thanks  
Greg

---

<div class="post-metadata">

**Author:** ![greg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/71c47a/32.png) [@greg](https://www.ruby-forum.com/u/greg)\
**Post date:** [July 17, 2007, 11:56am UTC](https://www.ruby-forum.com/t/can-active-record-support-find-parent-objects-but-filter/109600/2 "2007-07-17T11:56:00Z")

</div>

if you take your query  
and break it up into the following sections [:select, :from,  
:conditions]

then you’ll be able to do

find(:all,  
:select =\> “parent.id, parent.title, parent.description”,  
:from =\> “parent, child”,  
:conditions =\> [  
“parent.id = child.parent\_id AND child.memo like ?”,  
“%#{texttosearch}%”  
]  
)

Greg H. wrote:

> Hi,
> 
> Can active record handle the following? Or do I need to stick to  
> “.find\_by\_sql” to do this:
> 
> select parent.id, parent.title, parent.description from parent, child  
> where parent.id = child.parent\_id AND child.memo like  
> ‘%texttosearch%’
> 
> That is, I want to effectively ‘find’ parent objects, but filtered based  
> on  
> the contents of it’s associated child objects data. So the question is  
> if  
> it is possible what would the ruby code be to do this? i.e. results =  
> Parent.find(:all, …
> 
> Thanks  
> Greg
