# Sql queries in rails

**URL:** <https://www.ruby-forum.com/t/sql-queries-in-rails/137773>\
**Category:** Rails\
**Created:** [May 16, 2008, 7:25am UTC](https://www.ruby-forum.com/t/sql-queries-in-rails/137773 "2008-05-16T07:25:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 16, 2008, 7:25am UTC](https://www.ruby-forum.com/t/sql-queries-in-rails/137773/1 "2008-05-16T07:25:17Z")

</div>

I have an assignment and I need to be able to run a query inputted by  
the user (no matter what the query). Also I need to display the result  
cleanly with the field names if the query was a select query. So far I  
have

@result = ActiveRecord::Base.connection.execute(params[:query])

and in my view I have

\<% for result in @result %\>  
\<%= result %\>  
\<% end %\>

but this returns all the the data as a single line. Please help!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 16, 2008, 8:56am UTC](https://www.ruby-forum.com/t/sql-queries-in-rails/137773/2 "2008-05-16T08:56:18Z")

</div>

On May 16, 6:17 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have an assignment and I need to be able to run a query inputted by  
> the user (no matter what the query). Also I need to display the result  
> cleanly with the field names if the query was a select query. So far I  
> have
> 
> @result = ActiveRecord::Base.connection.execute(params[:query])

Use connection.select\_all and hope that the user doesn’t try and  
delete your database.

Fred
