我一直在试图找到了这个答案,也许是太简单了......迭代通过积极创纪录的业绩 - Ruby on Rails的
在轨道,是如何通过结果来遍历ActiveRecord的拉的最佳途径你想要的特定字段?
我有意见(命名职位)控制器,该控制器提取所有记录:
def index
@posts = Post.find(:all)
end
然后在索引视图,当我使用<%= @posts%>我得到的所有数据.. 。其中是伟大的......
#<Post id: 1, user_id: "9", picture: nil, comments: "here's a first comment", title: nil, twitter: nl, frame: nil, created_at: "2012-05-09 04:21:16", updated_at: "2012-05-09 04:21:16"> #<Post id: 2, user_id: "9", picture: nil, comments: "here's a second comment", title: nil, twitter: "please", frame: nil, created_at: "2012-05-09 05:20:03", updated_at: "2012-05-09 05:20:03">
如何我现在可以通过测试循环,使该视图显示了意见和created_at字段中的数据:
这里的第一个评论,2012-05-09 4时21分16秒
这里的第二个意见,2012-05-09五时20分03秒
我试过以下,并得到一个错误。
<% @posts.each do |c| %>
<%= c.posts.comments %>
<%= c.posts.created_at %>
<% end %>
那么,你得到的错误是什么? – DVG