我是rails的新手,我需要执行一些sql查询并将其输出到 View 。在我的 Controller 中,我使用了类似“@prob = ActiveRecord::Base.connection.execute("...")
的内容,然后简单地在 View 中显示@prob。但是 View 中的输出不断出现 0 结果列名称 result 的两倍。
sql 查询是
@cprob = ActiveRecord::Base.connection.execute("SELECT DISTINCT cause FROM pages LIMIT 2")
我的应用环境是
Ruby version 1.8.7 (i386-mingw32)
RubyGems version 1.3.6
Rack version 1.0
Rails version 2.3.5
Active Record version 2.3.5
Active Resource version 2.3.5
Action Mailer version 2.3.5
Active Support version 2.3.5
Application root C:/Users/Prateek/BitNami RubyStack projects/noc
Environment development
Database adapter sqlite3
Database schema version 20100616055513
更新:-我发现 ActiveRecord::Base.connection.execute 的输出实际上是一个包含像 {:0 => queryresult :columnname => queryresult} 这样的哈希值的数组
那么我如何从中获取查询结果
最佳答案
已执行
`@users = ActiveRecord::Base.connection.execute("SELECT * FROM users")`
在我的 Controller 中并获取用户名
<% @users.each do |user| %>
<td><%=h user[1] %></td>
<% end %>
在 View 中
希望能帮助您解决问题
关于ruby-on-rails - ActiveRecord::Base.connection.execute 重复 sql 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3177321/