Writing a SQL Query in Active Record?

A

Anukul Singhal

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
 
P

Phillip Gawlowski

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

Anukul Singhal 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

Look for #find, or #find_by_sql.

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: 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

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

Anukul Singhal

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

Anukul Singhal 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

Look for #find, or #find_by_sql.

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: 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

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
 
P

Phillip Gawlowski

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

Anukul Singhal 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 Gawlowski
Twitter: twitter.com/cynicalryan
Blog: 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

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

Simon Krahnke

* Anukul Singhal said:
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
 
P

Phillip Gawlowski

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

Simon Krahnke wrote:
|
|> 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?

http://en.wikipedia.org/wiki/Active_record_pattern


- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: 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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top