我是 Ruby on Rails 新手,对关联对象的视图逻辑有疑问:
我的模型看起来类似于
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
我想要显示的是所有帖子的列表以及每个帖子的前三个评论。
所以,我保持后控制器索引操作简单
class PostController < ApplicationController
#..
def index
@posts = Post.find(:all)
end
#..
end
现在在views/posts/index.html.erb
我可以做这样的事情@posts.comments
,我可以循环前三个条目。但是我如何访问通常在模型中完成的功能(在这种情况下是关联的模型),例如视图(或控制器)中的排序、范围等?