# Get parents via a condition on a child? (ActiveRecord)

**URL:** <https://www.ruby-forum.com/t/get-parents-via-a-condition-on-a-child-activerecord/137639>\
**Category:** Rails\
**Created:** [May 14, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/get-parents-via-a-condition-on-a-child-activerecord/137639 "2008-05-14T23:01:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vvildo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ed655f/32.png) [@vvildo](https://www.ruby-forum.com/u/vvildo)\
**Post date:** [May 14, 2008, 11:01pm UTC](https://www.ruby-forum.com/t/get-parents-via-a-condition-on-a-child-activerecord/137639/1 "2008-05-14T23:01:58Z")

</div>

I come from a PHP background where I worked without an ORM. I’m having  
some trouble converting what I know will work in raw SQL into  
ActiveRecord models.

I have the following two models (not my actual models, just as a generic  
example):

class Topic \< ActiveRecord::Base  
has\_many :posts  
…  
end

class Post \< ActiveRecord::Base  
belongs\_to :topic  
attr\_accessible :body  
…  
end

I want to work with the collection of Topics that have Posts with a  
certain value of :body. That is, “All topics that contain posts whose  
body is equal to ‘example’”

If I was working with straight SQL I would do this

SELECT topics.\* FROM  
topics JOIN posts ON topics.id = posts.topic\_id  
WHERE  
posts.body = “example”

In ActiveRecord I can get the list of Posts without a problem.

Post.find(:all, :conditions =\> {:body =\> “example”})

I think this is equivalent to the above

Post.find\_all\_by\_body(“example”)

What I can’t figure out is how to get the collection of Topics.

This doesn’t work because Topics don’t have :body’s

Topic.find(:all, :include =\> :posts, :conditions =\> {:body =\>  
“example”})

I’ve looked at the generated SQL and because the :posts table is aliased  
I don’t think I can come up with SQL fragment that would consistently  
work.

I can see how I may be able to first get the collection of Posts and  
then in another call to #find get the Topics I need, but that seems too  
roundabout.

What’s the best way to do this with ActiveRecord?

Thanks!

---

<div class="post-metadata">

**Author:** ![vvildo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ed655f/32.png) [@vvildo](https://www.ruby-forum.com/u/vvildo)\
**Post date:** [May 15, 2008, 12:57am UTC](https://www.ruby-forum.com/t/get-parents-via-a-condition-on-a-child-activerecord/137639/2 "2008-05-15T00:57:28Z")

</div>

In my limited understanding, I think if you managed to pull out the  
collection of Post, you have access to all Topic that are linked to  
that collection of Post.

I don’t know if this will work, I think the following example will  
print out the Topic name of each of the post that has body = example.

allpost = Post.find(:all, :conditions =\> {:body =\> “example”})

allpost.each do | onepost |  
puts onepost.topic.name  
end

On May 14, 2:01 pm, Walter Wilfinger \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![vvildo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ed655f/32.png) [@vvildo](https://www.ruby-forum.com/u/vvildo)\
**Post date:** [May 15, 2008, 1:32am UTC](https://www.ruby-forum.com/t/get-parents-via-a-condition-on-a-child-activerecord/137639/3 "2008-05-15T01:32:36Z")

</div>

So you want to find all the topics where a post in that topic has the  
body  
of “example”?

Topic.find(:all, :include =\> :posts, :conditions =\> [“posts.body =  
?”,params[:query]])
