# Inject SQL Statement in Rails

**URL:** <https://rubytalk.org/t/inject-sql-statement-in-rails/42941>\
**Category:** ruby-talk\
**Created:** [12 December 2007 03:22 UTC](https://rubytalk.org/t/inject-sql-statement-in-rails/42941 "2007-12-12T03:22:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jojo\_Mojo](https://avatars.discourse-cdn.com/v4/letter/j/f07891/32.png) [@Jojo\_Mojo](https://rubytalk.org/u/Jojo_Mojo)\
**Post date:** [12 December 2007 03:22 UTC](https://rubytalk.org/t/inject-sql-statement-in-rails/42941/1 "2007-12-12T03:22:30Z")

</div>

Hi,

I have a simple SQL statement which I would like to inject in my web  
application. The SQL statement is:

Select course\_id  
from degrees  
where programme\_id = 1

I need to know the step by step on how to make this work. What changes  
should be made to the model, view or controller? Urgent. Please reply  
asap.  
Thank you very much!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![yudi](https://avatars.discourse-cdn.com/v4/letter/y/34f0e0/32.png) [@yudi](https://rubytalk.org/u/yudi)\
**Post date:** [12 December 2007 03:34 UTC](https://rubytalk.org/t/inject-sql-statement-in-rails/42941/2 "2007-12-12T03:34:08Z")

</div>

You can use find\_by\_sql to use MySQL directly:

@object.find\_by\_sql("SELECT course\_id  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM degrees  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE programme\_id = 1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")

@object will be referenced with an array of entries from database.

> **···**
>
> On Dec 11, 2007 9:22 PM, Jojo Mojo \<lonexenophobia@hotmail.com\> wrote:
> 
> > Hi,
> > 
> > I have a simple SQL statement which I would like to inject in my web  
> > application. The SQL statement is:
> > 
> > Select course\_id  
> > from degrees  
> > where programme\_id = 1
> > 
> > I need to know the step by step on how to make this work. What changes  
> > should be made to the model, view or controller? Urgent. Please reply  
> > asap.  
> > Thank you very much!  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> Do not be afraid of Change.

---

<div class="post-metadata">

**Author:** ![Jojo\_Mojo](https://avatars.discourse-cdn.com/v4/letter/j/f07891/32.png) [@Jojo\_Mojo](https://rubytalk.org/u/Jojo_Mojo)\
**Post date:** [13 December 2007 00:30 UTC](https://rubytalk.org/t/inject-sql-statement-in-rails/42941/3 "2007-12-13T00:30:23Z")

</div>

yudi wrote:

> You can use find\_by\_sql to use MySQL directly:
> 
> @object.find\_by\_sql("SELECT course\_id  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;FROM degrees  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WHERE programme\_id = 1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;")
> 
> @object will be referenced with an array of entries from database.

okay so i put this statement in the model right?

just curious, how does the view/controller know where to find this?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![yudi](https://avatars.discourse-cdn.com/v4/letter/y/34f0e0/32.png) [@yudi](https://rubytalk.org/u/yudi)\
**Post date:** [13 December 2007 01:21 UTC](https://rubytalk.org/t/inject-sql-statement-in-rails/42941/4 "2007-12-13T01:21:11Z")

</div>

Check RailsEnvy for an ActiveRecord tutorial. You'll find everything you  
need over therehttp://www.railsenvy.com/2007/8/8/activerecord-tutorial

> **···**
>
> On Dec 12, 2007 6:30 PM, Jojo Mojo \<lonexenophobia@hotmail.com\> wrote:
> 
> > yudi wrote:  
> > \> You can use find\_by\_sql to use MySQL directly:  
> > \>  
> > \> @object.find\_by\_sql("SELECT course\_id  
> > \> FROM degrees  
> > \> WHERE programme\_id = 1  
> > \> ")  
> > \>  
> > \> @object will be referenced with an array of entries from database.
> > 
> > okay so i put this statement in the model right?
> > 
> > just curious, how does the view/controller know where to find this?  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> Do not be afraid of Change.

---

<div class="post-metadata">

**Author:** ![Jojo\_Mojo](https://avatars.discourse-cdn.com/v4/letter/j/f07891/32.png) [@Jojo\_Mojo](https://rubytalk.org/u/Jojo_Mojo)\
**Post date:** [13 December 2007 01:33 UTC](https://rubytalk.org/t/inject-sql-statement-in-rails/42941/5 "2007-12-13T01:33:11Z")

</div>

thanks everyone for all the help!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
