# Writing a SQL Query in Active Record?

**URL:** <https://www.ruby-forum.com/t/writing-a-sql-query-in-active-record/137065>\
**Category:** Ruby\
**Created:** [May 8, 2008, 5:16pm UTC](https://www.ruby-forum.com/t/writing-a-sql-query-in-active-record/137065 "2008-05-08T17:16:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [May 8, 2008, 5:16pm UTC](https://www.ruby-forum.com/t/writing-a-sql-query-in-active-record/137065/1 "2008-05-08T17:16:02Z")

</div>

Hi,

I am using the following code snippet for connecting to SQL Server  
through Active Record:

require ‘rubygems’  
require ‘active\_record’

ActiveRecord::Base.establish\_connection(  
:adapter =\> “sqlserver”,  
:host =\> “xxx.xxx.xxx.xxx,8080”  
:username =\> “username”  
:password =\> “password”  
:database =\> “xyz\_1008”  
)

Now, I want to execute a query in this sql server after getting  
successfully connected but am not aware how to do so. Can anyone give an  
example as to how we write the query say (“Select \* from Name”) after  
establishing the above connection successfully?

Thanks,  
Anukul

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [May 8, 2008, 5:33pm UTC](https://www.ruby-forum.com/t/writing-a-sql-query-in-active-record/137065/2 "2008-05-08T17:33:11Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Anukul S. wrote:  
| Hi,  
|  
| I am using the following code snippet for connecting to SQL Server  
| through Active Record:  
|  
| require ‘rubygems’  
| require ‘active\_record’  
|  
| ActiveRecord::Base.establish\_connection(  
| :adapter =\> “sqlserver”,  
| :host =\> “xxx.xxx.xxx.xxx,8080”  
| :username =\> “username”  
| :password =\> “password”  
| :database =\> “xyz\_1008”  
| )  
|  
| Now, I want to execute a query in this sql server after getting  
| successfully connected but am not aware how to do so. Can anyone give an  
| example as to how we write the query say (“Select \* from Name”) after  
| establishing the above connection successfully?

[ar.rubyonrails.com](http://ar.rubyonrails.com)

Look for #find, or #find\_by\_sql.

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

You write with ease to show your breeding,  
But easy writing 's curst hard reading.  
~ – Richard Brinsley Sheridan (1751-1816)  
~ – Clio’s Protest, Life of Sheridan (Moore), Vol. i, p. 155  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkgjHOcACgkQbtAgaoJTgL9k8QCdHTVwPREoYlByjns1t1Sr9NOY  
eYYAmgJ5A9zOn9ZrX8KgAUXwSz6oLORz  
=KK/C  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [May 8, 2008, 5:55pm UTC](https://www.ruby-forum.com/t/writing-a-sql-query-in-active-record/137065/3 "2008-05-08T17:55:33Z")

</div>

Phillip G. wrote:

> -----BEGIN PGP SIGNED MESSAGE-----  
> Hash: SHA1
> 
> Anukul S. wrote:  
> | Hi,  
> |  
> | I am using the following code snippet for connecting to SQL Server  
> | through Active Record:  
> |  
> | require ‘rubygems’  
> | require ‘active\_record’  
> |  
> | ActiveRecord::Base.establish\_connection(  
> | :adapter =\> “sqlserver”,  
> | :host =\> “xxx.xxx.xxx.xxx,8080”  
> | :username =\> “username”  
> | :password =\> “password”  
> | :database =\> “xyz\_1008”  
> | )  
> |  
> | Now, I want to execute a query in this sql server after getting  
> | successfully connected but am not aware how to do so. Can anyone give an  
> | example as to how we write the query say (“Select \* from Name”) after  
> | establishing the above connection successfully?
> 
> [ar.rubyonrails.com](http://ar.rubyonrails.com)
> 
> Look for #find, or #find\_by\_sql.
> 
> * * *
> 
> Phillip G.  
> Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)  
> Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)
> 
> You write with ease to show your breeding,  
> But easy writing 's curst hard reading.  
> ~ – Richard Brinsley Sheridan (1751-1816)  
> ~ – Clio’s Protest, Life of Sheridan (Moore), Vol. i, p. 155  
> -----BEGIN PGP SIGNATURE-----  
> Version: GnuPG v1.4.8 (MingW32)  
> Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)
> 
> iEYEARECAAYFAkgjHOcACgkQbtAgaoJTgL9k8QCdHTVwPREoYlByjns1t1Sr9NOY  
> eYYAmgJ5A9zOn9ZrX8KgAUXwSz6oLORz  
> =KK/C  
> -----END PGP SIGNATURE-----

Hi,

Thanks for the reply.

I am not using rails. Want to run the query only using Active Record on  
Ruby (Scite Editor)

Can you give any prototype as to how I can proceed writing a simple  
“SELECT” query after establishing the connection with the sql server db?

Thanks,  
Anukul

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [May 8, 2008, 7:41pm UTC](https://www.ruby-forum.com/t/writing-a-sql-query-in-active-record/137065/4 "2008-05-08T19:41:15Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Anukul S. wrote:

| I am not using rails. Want to run the query only using Active Record on  
| Ruby (Scite Editor)

Yes, that is what ActiveRecord’s #find and #find\_by\_sql methods do.

Rails uses ActiveRecord, but ActiveRecord works without using Rails  
itself.

| Can you give any prototype as to how I can proceed writing a simple  
| “SELECT” query after establishing the connection with the sql server db?

class Table \< ActiveRecord::Base; end

select = Table.find :all

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

~ How many boards would the Mongols hoard if the Mongol hordes got  
bored?  
– Calvin  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkgjOwUACgkQbtAgaoJTgL8NVQCgpLIP+PbwTxDnEhXiXDg3sLlc  
gy0An3GewesORQjT2Q7uQlvbEpUuRyi4  
=8Svr  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [May 8, 2008, 8:08pm UTC](https://www.ruby-forum.com/t/writing-a-sql-query-in-active-record/137065/5 "2008-05-08T20:08:22Z")

</div>

- Anukul S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (17:55) schrieb:

> I am not using rails. Want to run the query only using Active Record on  
> Ruby (Scite Editor)

What’s ActiveRecord for if you just want to use SQL?

> Can you give any prototype as to how I can proceed writing a simple  
> “SELECT” query after establishing the connection with the sql server db?

The normal ActiveRecord way is to create a Class and let it do that.

mfg, simon … l

---

<div class="post-metadata">

**Author:** ![anukulsinghal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dbc845/32.png) [@anukulsinghal](https://www.ruby-forum.com/u/anukulsinghal)\
**Post date:** [May 8, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/writing-a-sql-query-in-active-record/137065/6 "2008-05-08T20:16:40Z")

</div>

-----BEGIN PGP SIGNED MESSAGE-----  
Hash: SHA1

Simon K. wrote:  
| \* Anukul S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (17:55) schrieb:  
|  
|\> I am not using rails. Want to run the query only using Active Record on  
|\> Ruby (Scite Editor)  
|  
| What’s ActiveRecord for if you just want to use SQL?

> **[Active record pattern](https://en.wikipedia.org/wiki/Active_record_pattern)**
>
> In software engineering, the active record pattern is an architectural pattern. It is found in software that stores in-memory object data in relational databases. It was named by Martin Fowler in his 2003 book Patterns of Enterprise Application Architecture. The interface of an object conforming to this pattern would include functions such as Insert, Update, and Delete, plus properties that correspond more or less directly to the columns in the underlying database table.
> The active record patter...

* * *

Phillip G.  
Twitter: [twitter.com/cynicalryan](http://twitter.com/cynicalryan)  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

Parenthesise to avoid ambiguity.  
~ - The Elements of Programming Style (Kernighan & Plaugher)  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (MingW32)  
Comment: Using GnuPG with Mozilla - [http://enigmail.mozdev.org](http://enigmail.mozdev.org)

iEYEARECAAYFAkgjQzwACgkQbtAgaoJTgL8rbwCghN8DPR/rsWP4/sqedGKGXakq  
vH8AnjQZvMCyAel3WC991pKY12VsE9wY  
=zw3a  
-----END PGP SIGNATURE-----
