Ruby on rails 3 Rails 3:限制多对多关系

Ruby on rails 3 Rails 3:限制多对多关系,ruby-on-rails-3,activerecord,many-to-many,Ruby On Rails 3,Activerecord,Many To Many,我有一种多对多的关系:用户----产品。然而,我只希望产品的评论对其作者可见 路线是: resources :products do resources :comments end CommentsController的Index操作中的查询类似于: # @product loaded in before_filter @comments = @product.comments.where(:author_id=>current_user) # returns 0 or 1 reco

我有一种多对多的关系:用户----产品。然而,我只希望产品的评论对其作者可见

路线是:

resources :products do
  resources :comments
end
CommentsController的Index操作中的查询类似于:

# @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
如果没有评论,这可能会自动转到新操作,但我一直无法让它工作


这对于这个用户案例来说是合理的方法吗?

如果没有评论,您可以在show方法中重定向到。

有效(与单条路线相结合)。确切的语法是重定向到(:action=>'new'),并返回if@comment.nil?