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

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-

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]])

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs