2011-03-29 129 views
0

我有一个多对多的关系:用户 - <评论> - 产品。但是,我只希望产品的评论对其作者可见。Rails 3:限制多对多关系

的路线是:

resources :products do 
    resources :comments 
end 

在CommentsController的索引操作的查询会是这样的:

# @product loaded in before_filter 
@comments = @product.comments.where(:author_id=>current_user) # returns 0 or 1 records 

我可以在路线更改为:

resources :products do 
    resource :comments 
end 

然后使用显示操作:

# @product loaded in before_filter 
@comment = @product.comments.where(:author_id=>current_user) # returns 0 or 1 records 

如果没有评论,这可能应该自动路由到新的行动,但我一直没有能够得到这个工作。

这是一个合理的方法,这个用户案例?

回答

0

如果没有评论,你可以在show-method中做一个redirect_to。

+0

这工作(结合奇异路线)。确切的语法是redirect_to(:action =>'new')并返回if @ comment.nil? – craig 2011-03-29 13:57:42