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

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

class Topic < ActiveRecord::Base
has_many :posts


class Post < ActiveRecord::Base
belongs_to :topic
attr_accessible :body


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 = posts.topic_id
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


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 =>

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

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

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


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 |

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

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

Topic.find(:all, :include => :posts, :conditions => [“posts.body =