Display/View result of user sql stmt after find_by_sql

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?

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?

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

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

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/

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!

you can try this:

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-

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(/sSELECTs**sFROM.*/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