开发者

ActiveRecord::Base.connection.execute duplicates sql output

开发者 https://www.devze.com 2023-01-05 22:06 出处:网络
I am a newbie to rails and i needed to execute some sql query and output it to the view. In my controller i used something like \"@prob = ActiveRecord::Base.connection.execute(\"...\") and then simply

I am a newbie to rails and i needed to execute some sql query and output it to the view. In my controller i used something like "@prob = ActiveRecord::Base.connection.execute("...") and then simply displayed the @prob in the view. However the output in the view keeps coming twice as 0 result column name result .

The sql query was

 @cprob = ActiveRecord::Base.connection.execute("SELECT DISTINCT cause FROM pages LIMIT 2")

and my application environment is

Ruby version    开发者_JAVA百科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

Update : - I found out that the oputput for ActiveRecord::Base.connection.execute is actually an array containing hashes like {:0 => queryresult :columnname => queryresult}

so how do i get just the queryresult from this


Executed

`@users = ActiveRecord::Base.connection.execute("SELECT * FROM users")` 

in my controller and get user names as

<% @users.each do |user| %> <td><%=h user[1] %></td> <% end %>

in view

Hope it'll help you to solve your problem

0

精彩评论

暂无评论...
验证码 换一张
取 消