# Display/View result of user sql stmt after find\_by\_sql

**URL:** <https://www.ruby-forum.com/t/display-view-result-of-user-sql-stmt-after-find-by-sql/103783>\
**Category:** Rails\
**Created:** [May 10, 2007, 9:39am UTC](https://www.ruby-forum.com/t/display-view-result-of-user-sql-stmt-after-find-by-sql/103783 "2007-05-10T09:39:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kiran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/13edae/32.png) [@kiran](https://www.ruby-forum.com/u/kiran)\
**Post date:** [May 10, 2007, 9:39am UTC](https://www.ruby-forum.com/t/display-view-result-of-user-sql-stmt-after-find-by-sql/103783/1 "2007-05-10T09:39:36Z")

</div>

I am trying to support free-form SQL in an internal website and just  
take the string and pass it to  
find\_by\_sql. It works fine. But, to show the result I am using the  
attributes method of the ActiveRecord:Base class like thus:

\<% for task in @tasks %\>  
\<% if (first == 1) %\>  
\<% first = 0%\>

```
  <% for column in task.attributes %>
    <th><%= column[0] %></th>
  <% end %>
</tr>

```

\<% end %\>

 "\> \<% for column in task.attributes %\> \<%= column[1] %\> \<% end %\> 

\<% end %\>

The funny thing is that the order of columns returned by the  
attributes call is neither alphabetical nor the order specified by the  
SELECT clause.

What I really would like to do is to display the columns in the order  
of the SELECT clause. Any easy way to do this other than parse SELECT  
on my own again?

---

<div class="post-metadata">

**Author:** ![kiran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/13edae/32.png) [@kiran](https://www.ruby-forum.com/u/kiran)\
**Post date:** [May 16, 2007, 11:13pm UTC](https://www.ruby-forum.com/t/display-view-result-of-user-sql-stmt-after-find-by-sql/103783/2 "2007-05-16T23:13:55Z")

</div>

kiran wrote:

> I am trying to support free-form SQL in an internal website and just  
> take the string and pass it to find\_by\_sql.

I have the same problem, and no idea for a solution - can anyone else  
help?  
Kiran, did you work anything out?

---

<div class="post-metadata">

**Author:** ![kiran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/13edae/32.png) [@kiran](https://www.ruby-forum.com/u/kiran)\
**Post date:** [May 16, 2007, 11:59pm UTC](https://www.ruby-forum.com/t/display-view-result-of-user-sql-stmt-after-find-by-sql/103783/3 "2007-05-16T23:59:30Z")

</div>

Nope. I am waiting for lightning to strike or to start parsing SELECT  
myself ☹

On May 16, 2:13 pm, Matthew Fallshaw \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![kiran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/13edae/32.png) [@kiran](https://www.ruby-forum.com/u/kiran)\
**Post date:** [June 20, 2007, 7:43pm UTC](https://www.ruby-forum.com/t/display-view-result-of-user-sql-stmt-after-find-by-sql/103783/4 "2007-06-20T19:43:19Z")

</div>

Okay, so I figured out a way on my own: Please see my blog post for  
the details:

[http://www.kiran.srilatha.com/bondlog/2007/06/19/running-free-on-rails-displaying-results-of-find\_by\_sql-queries/](http://www.kiran.srilatha.com/bondlog/2007/06/19/running-free-on-rails-displaying-results-of-find_by_sql-queries/)

---

<div class="post-metadata">

**Author:** ![kiran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/13edae/32.png) [@kiran](https://www.ruby-forum.com/u/kiran)\
**Post date:** [June 20, 2007, 8:22pm UTC](https://www.ruby-forum.com/t/display-view-result-of-user-sql-stmt-after-find-by-sql/103783/5 "2007-06-20T20:22:26Z")

</div>

Very nice, I am trying to lean RoR with experimentation and reading  
this forum, could you post the code that would gather the find\_by\_sql  
varible?

Thanks!

---

<div class="post-metadata">

**Author:** ![kiran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/13edae/32.png) [@kiran](https://www.ruby-forum.com/u/kiran)\
**Post date:** [June 20, 2007, 7:44pm UTC](https://www.ruby-forum.com/t/display-view-result-of-user-sql-stmt-after-find-by-sql/103783/6 "2007-06-20T19:44:19Z")

</div>

you can try this:

[http://www.kiran.srilatha.com/bondlog/2007/06/19/running-free-on-rails-displaying-results-of-find\_by\_sql-queries/](http://www.kiran.srilatha.com/bondlog/2007/06/19/running-free-on-rails-displaying-results-of-find_by_sql-queries/)

On May 16, 2:13 pm, Matthew Fallshaw \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![kiran](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/13edae/32.png) [@kiran](https://www.ruby-forum.com/u/kiran)\
**Post date:** [June 23, 2007, 8:04am UTC](https://www.ruby-forum.com/t/display-view-result-of-user-sql-stmt-after-find-by-sql/103783/7 "2007-06-23T08:04:20Z")

</div>

Can you clarify what exactly you are asking? I thought there were enough  
details in the blog post?

If you cannot see blog here the raw code without the wisdom:

def sqlquery  
if (!querystr.blank?)  
@results = Model.find\_by\_sql(querystr)  
@total = @results.size()  
if (@total \> 0)  
cols = @results.first.attribute\_names  
if (querystring.match(/s_SELECTs\*\*s_FROM.\*/i))  
@columns = cols  
else  
pos = cols.inject({}) { |h, col| h[col] =  
querystr.index(col); h}  
@columns = cols.sort { |x,y| pos[x] \< =\> pos[y]}  
end  
else  
@total = 0  
end

table  
thead  
\< % for col in @columns %\>  
td \< %= col %\> /td  
\< % end %\>  
/tr  
/thead  
\< % for item in @results %\>  
tr  
\< % for col in @columns %\>  
td \< %= item[col] %\> /td  
\< % end %\>  
/tr  
\< % end %\>  
/table  
end
