Say I have a post model that has_and_belongs_to_many :categories, and a category model that has_and_belongs_to_many :posts. So I have a categories_posts table to do the join. That''s all find and dandy. However I want to be able to find all the posts that belong to a specific category. I tried Post.find_by_category(:id) but that doesn''t work. Does anyone have any insight? Thanks in advance. -- Posted via http://www.ruby-forum.com/.
Hi Micheal Try it the other way around Category.find( some id).posts On 4/6/06, Michael Leung <blamemike@gmail.com> wrote:> > > Say I have a post model that has_and_belongs_to_many :categories, and a > category model that has_and_belongs_to_many :posts. So I have a > categories_posts table to do the join. That''s all find and dandy. > However I want to be able to find all the posts that belong to a > specific category. > > I tried Post.find_by_category(:id) but that doesn''t work. > > Does anyone have any insight? > > Thanks in advance. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060406/a2faf937/attachment.html
How about... Category.find(:id).posts ~ Ben On 4/5/06, Michael Leung <blamemike@gmail.com> wrote:> > > Say I have a post model that has_and_belongs_to_many :categories, and a > category model that has_and_belongs_to_many :posts. So I have a > categories_posts table to do the join. That''s all find and dandy. > However I want to be able to find all the posts that belong to a > specific category. > > I tried Post.find_by_category(:id) but that doesn''t work. > > Does anyone have any insight? > > Thanks in advance. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Ben Reubenstein http://www.benr75.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060406/3fadfd95/attachment.html