Topic: searching through belongs_to relations

Hey all,
I have a Topic and a Post model, topics have many posts and posts belongs to a topic.
I would like to find all posts which topic name = 'xyz'. How could I do that?

those are my models attributes:
Topic(id,name)
Post(id,content,topic_id)

so I guess it would something like:
Post.find(:all,:conditions=> something with topic_id and topic.name smile )

thanx in advance

Pat

Re: searching through belongs_to relations

Try:

Post.find(:all, :include => :topic, :conditions => ['topics.name = ?', name])

Railscasts - Free Ruby on Rails Screencasts

Re: searching through belongs_to relations

ok thanx,
and what if I have
Category(id,name)
Forum(id,category_id)
Topic(id,name,forum_id)
Post(id,content,topic_id)

Category has_many forums
Forum has_many topics which belongs to category
Topic has_many posts which belongs_to forum
Post which belongs_to Topic

how can I find all posts who belongs to category 'xyz' or to topic 'zrt' or forum 'uyi' ?

thanx in advance

Pat

Re: searching through belongs_to relations

I don't think Rails allows you to go that deep, but I could be wrong. You may want to try playing around with the has_one :through relationship in Post and see if find :include works through that. If not, you may need to use find_by_sql and build the joins yourself.

Railscasts - Free Ruby on Rails Screencasts