假设Rails嵌套has_many关联,如何获得所有孩子的最后5个?
Post has_many :comments
和
Comment has_many :ratings
我怎样才能抓住每个帖子的最后5个评论收视率?我一直在考虑循环评论每篇文章,但这并不能解决最后5部分。
编辑:为响应J.,因为我似乎无法格式化注释字段
里面的代码你可以嵌套:通过关系?说...
class Category < ActiveRecord::Base
has_many :posts
has_many :comments, :through => posts
has_many :ratings, :through => comments
end
class Post < ActiveRecord::Base
belongs_to :category
has_many :comments
has_many :ratings, :through => comments
end
class Comment < ActiveRecord::Base
belongs_to :post
has_many :ratings
end
class Rating < ActiveRecord::Base
belongs_to :comment
end
要回答你的编辑,我米不知道你可以做到这一点。至少我从来没有这样做过......在我看到你的编辑后,我实际上正在尝试做,但没有成功。 – 2010-06-09 19:11:56
人,我想知道为什么。嵌套这种关系听起来应该更常见。我应该重新考虑我的数据模型吗? – Randuin 2010-06-09 19:34:05
我不是说这是不可能的,我只是不能在这里做...你应该尝试看看你得到了什么:] – 2010-06-09 20:04:35