0

我有一个Author模型与name作为一个字段 - 它has_many文章。Rails嵌套路径,不是资源

我也有一个Article模型namedescription的领域 - 它belongs_to作者和has_many评论。

最后,我有一个Comment模式,用comment_textarticle_id的领域 - 它belongs_to条,

我寻找筑巢的路径我。即,(authors /:id/comments) 如何嵌套路径,但不是资源?为了澄清,我不想在评论模型中使用author_id,但是当我访问authors /:id/comments时,我应该能够看到与作者相关的所有文章并对它们发表评论。

+0

非常感谢Zoran,重新设计我的问题。 –

回答

1

作者

has_many :articles 
has_many :comments, :through => :articles (This line will give you comments for the author) 

belongs_to :author (means it has author_id as a field) 
has_many :comments 

评论

belongs_to :article 

如果上述结构是有沿y我们的模型,那么你可以做:

@author = Author.where(:id => some_id) 
@comments = @author.comments 

你并不需要有在评论表AUTHOR_ID

+0

非常感谢Abhi,有没有一种方法可以从url:authors /:some_id/comments获得'some_id'。 –

+0

我明白了,通过has_many嵌套资源来达到魔法。 –

+0

@ManjunathP酷...如果这个答案有帮助,接受它作为正确的答案,以便它可以帮助他人在未来 – Abhi