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:
<verbatim>
<% for task in @tasks %>
<% if (first == 1) %>
<% first = 0%>
<thead>
<tr>
<% for column in task.attributes %>
<th><%= column[0] %></th>
<% end %>
</tr>
<% end %>
<tr class="<%= cycle("even", "odd")
%>">
<% for column in task.attributes %>
<td><%= column[1] %></td>
<% end %>
</tr>
<% end %>
</table>
</verbatim>
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?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Matthew Fallshaw
2007-May-16 21:13 UTC
Re: Display/View result of user sql stmt after find_by_sql
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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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- s.net> wrote:> kiran wrote: > > I am trying to support free-form SQL in an internal website and just > > take the string and pass it tofind_by_sql. > > I have the same problem, and no idea for a solution - can anyone else > help? > Kiran, did you work anything out? > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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/ On May 16, 2:58 pm, kiran <kiran.bondalap...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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- > > s.net> wrote: > >kiranwrote: > > > I am trying to support free-form SQL in an internal website and just > > > take the string and pass it tofind_by_sql. > > > I have the same problem, and no idea for a solution - can anyone else > > help? > >Kiran, did you work anything out? > > > -- > > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
john95127
2007-Jun-20 18:21 UTC
Re: Display/View result of user sql stmt after find_by_sql
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! On Jun 20, 10:42 am, kiran <kiran.bondalap...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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-rail... > > On May 16, 2:58 pm, kiran <kiran.bondalap...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > 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- > > > s.net> wrote: > > >kiranwrote: > > > > I am trying to support free-form SQL in an internal website and just > > > > take the string and pass it tofind_by_sql. > > > > I have the same problem, and no idea for a solution - can anyone else > > > help? > > >Kiran, did you work anything out? > > > > -- > > > Posted viahttp://www.ruby-forum.com/.- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Kiran Bondalapati
2007-Jun-23 06:03 UTC
Re: Display/View result of user sql stmt after find_by_sql
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
On 6/20/07, john95127
<john.steinbach-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
>
> 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!
>
> On Jun 20, 10:42 am, kiran
<kiran.bondalap...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> > 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-rail...
> >
> > On May 16, 2:58 pm, kiran
<kiran.bondalap...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> >
> >
> >
> > > 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-
> >
> > > s.net> wrote:
> > > >kiranwrote:
> > > > > I am trying to support free-form SQL in an internal
website and
> just
> > > > > take the string and pass it tofind_by_sql.
> >
> > > > I have the same problem, and no idea for a solution - can
anyone
> else
> > > > help?
> > > >Kiran, did you work anything out?
> >
> > > > --
> > > > Posted viahttp://www.ruby-forum.com/.- Hide quoted text -
> >
> > - Show quoted text -
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---