【发布时间】:2008-11-11 21:02:26
【问题描述】:
我有三个(相关)模型,指定如下:
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 以获取带有路线的 user - 假设这是为了对所有帖子的 cmets 进行批量批准。 (注意你也可以得到user发的comments,但是用户不能对自己的帖子发表评论,所以comments通过post是不同的,互斥的)。从逻辑上讲,这应该适用于:
map.resources :users, :has_many => [:posts, :comments, :comments_received]
这应该给我路线
user_posts_path
user_comments_path
user_comments_received_path
前两个有效,最后一个无效。我试过没有comments_received 中的_,但无济于事。我正在寻找类似的网址
http://some_domain.com/users/123/comments_received
我也尝试过嵌套它,但也许我做错了。在这种情况下,我认为地图将是:
map.resources :users do |user|
user.resources :comments
user.resources :posts, :has_many => :comments
end
然后网址可能是:
http://some_domain.com/users/123/posts/comments
也许这是正确的做法,但我的语法错误?
我是不是想错了?对我来说,我应该能够将所有comments 添加到所有用户的帖子中,这似乎是合理的。
感谢您的帮助!
【问题讨论】:
标签: ruby-on-rails resources map has-many