English 中文(简体)
通过积极的记录结果——铁路的废墟
原标题:iterate through active record results - ruby on rails

我曾试图找到这一答案,可能过于简单。

在铁路方面,如何通过积极记录取得的结果,使你想要的具体领域得到振兴?

我有一位评论员(指定员额),负责记录所有记录:

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"> 

我现在如何通过测试使这种观点能够显示评论中的数据,并创造:

The first comment, 2012-05-09 04:21:16

The second comment, 2012-05-09 05:20:03

我尝试了以下几个错误。

<% @posts.each do |c| %>
   <%= c.posts.comments %>
   <%= c.posts.created_at %>
<% end %>
最佳回答

在<代码>@posts.each do >>上,“c”是指在<代码>@posts的收集中的具体职位标的。

因此,从某种意义上来说,你正在努力做到:<代码><%=员额.posts.comments %>

在这方面,该守则应如何看待:

<% @posts.each do |p| %>
   <%= p.comments %>
   <%= p.created_at %>
<% end %>
问题回答

改变现状:

<% @posts.each do |post| %>
   <%= post.comments %>
   <%= post.created_at %>
<% end %>

我认为,如果你把内变数称为外变数的单体,那么人们就更容易效仿——因此,外部的<代码>@posts成为post

亲爱!





相关问题
rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

SSL slowness in EC2

We ve deployed our rails app to EC2. In our setup, we have two proxies on small instances behind round-robin DNS. These run nginx load balancers for a dynamically growing and shrinking farm of web ...

Auth-code with A-Za-z0-9 to use in an URL parameter

As part of a web application I need an auth-code to pass as a URL parameter. I am currently using (in Rails) : Digest::SHA1.hexdigest((object_id + rand(255)).to_s) Which provides long strings like : ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

activerecord has_many :through find with one sql call

I have a these 3 models: class User < ActiveRecord::Base has_many :permissions, :dependent => :destroy has_many :roles, :through => :permissions end class Permission < ActiveRecord::...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签