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!