rails map.resources与has_many:通过不起作用? [英] rails map.resources with has_many :through doesn't work?

查看:139
本文介绍了rails map.resources与has_many:通过不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个(相关)型号,指定如下:

I've got three (relevant) models, specified like this:

class User < ActiveRecord::Base
  has_many :posts
  has_many :comments
  has_many :comments_received, :through => :posts, :source => :comments
end

class Post < ActiveRecord::Base
  belongs_to :user
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :user
  belongs_to :post
end

我想能够使用路线引用用户的所有 comments_received - 假设它是批准批准所有职位的意见。 (请注意,您也可以从用户获取评论,但用户不能评论他们自己的帖子,所以注释通过 post 是不同的,互斥的)。在逻辑上,这应该适用于:

I'd like to be able to reference all the comments_received for a user with a route - let's say it's for batch approval of comments on all posts. (note that you can also get comments made by the user, but users can't comment on their own posts, so the comments through a post are different and mutually exclusive). Logically, this should work with:

map.resources :users, :has_many => [:posts, :comments, :comments_received]

这应该给我路线

user_posts_path
user_comments_path
user_comments_received_path

前两个工作,最后一个没有。我试过没有 _ comments_received 无效。我正在寻找一个像

The first two work, the last one doesn't. I've tried it without the _ in comments_received to no avail. I'm looking to get a URL like

http://some_domain.com/users/123/comments_received

我也尝试嵌套它,但也许我在做错了。在这种情况下,我会认为地图将是:

I've also tried nesting it, but maybe I'm doing that wrong. In that case, I'd think the map would be:

map.resources :users do |user|
  user.resources :comments
  user.resources :posts, :has_many => :comments
end

然后url可能是:

http://some_domain.com/users/123/posts/comments

也许这是正确的方法,但我有错误的语法?

Maybe this is the right way to do it, but I have the syntax wrong?

我想这是错误的办法?对于我来说,我应该能够获得一个页面,将所有评论添加到用户的所有帖子中。

Am I thinking about this the wrong way? It seems reasonable to me that I should be able to get a page of all the comments added to all of a user's posts.

感谢您的帮助!

推荐答案

虽然deinfe的语法资源和模型的关系是相似的,你不应该被愚弄认为资源映射到一个模型。阅读 David Black不得不说

Although the syntax to deinfe resource and model relationship is similar, you shouldn't be fooled into thinking that a resource maps to a model. Read what David Black has to say.

您遇到的问题是您要生成的路线。使用这样的嵌套语法:

The problem you're having is with the routes you're generating. Using the nested syntax like so:

map.resources :users do |user|
  user.resources :posts
  user.resources :comments
  user.resources :comments_received
end

然后运行'rake routes',给我(其他东西的负载!):

And then running 'rake routes', gives me (amongst loads of other stuff!):

                       users GET /users                              {:action=>"index", :controller=>"users"}
                  user_posts GET /users/:user_id/posts               {:action=>"index", :controller=>"posts"}
               user_comments GET /users/:user_id/comments            {:action=>"index", :controller=>"comments"}
user_comments_received_index GET /users/:user_id/comments_received   {:action=>"index", :controller=>"comments_received"}

所以看来,rails将_index添加到comments_received路由的末尾。我会承认我不知道为什么(与其他评论路线冲突有什么关系),但它解释了你的问题。

So it appears that rails is adding _index to the end of the comments_received route. I'll admit I don't know why (something to do with clashing with the other comments route?) but it explains your problem.

一个更好的选择可能是定义对您的注释资源的收集操作,如下所示:

An nicer alternative might be to define a collection action on your comments resource, like so:

map.resources :users do |user|
  user.resources :posts
  user.resources :comments, :collection => {:received => :get}
end

这将给你以下路线:

                 users GET /users                             {:action=>"index", :controller=>"users"}
            user_posts GET /users/:user_id/posts              {:action=>"index", :controller=>"posts"}
         user_comments GET /users/:user_id/comments           {:action=>"index", :controller=>"comments"} 
received_user_comments GET /users/:user_id/comments/received  {:action=>"received", :controller=>"comments"}

注意:收到的动作现在在评论控制器上

Note: the received action is now on the comments controller

这篇关于rails map.resources与has_many:通过不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆