Connecting to SQLServer

48 views
Skip to first unread message

Steve Dc

unread,
Jun 8, 2009, 11:23:20 AM6/8/09
to rubyonra...@googlegroups.com
New to rails. Installed rails on my XP desktop, and setup my first app
that will use an existing SQLServer database. I am able to connect to
SQLServer using odbc with following code.


require 'rubygems'
require 'ActiveRecord'

ActiveRecord::Base.establish_connection(
:adapter => 'sqlserver',
:mode => 'odbc',
:dsn => 'Driver={SQL Server};Server=<server>;Database=<db>',
:username => 'uid',
:password => 'pwd'
)

class src <ActiveRecord::Base
set_table_name 'Source'
set_primary_key 'IDSource'
end

s = []
s = Source.find(1)
puts s.SourceName


I was able to connect and the code behaves as expected. However when I
try to use database.yml to specify the same connection information, I am
unable to connect using script/dbconsole.

#database.yml
development:
adapter : sqlserver
mode : odbc
dsn : Driver={SQL Server};Server=<server>;Database=<db>
username : <uid>
password : <pwd>

When I start script/dbconsole, the error is
D:\work\rails\job>ruby script/dbconsole
Unknown command-line client for <db>. Submit a Rails patch to add
support!

My assumption is that my database.yml does not describe the connection
properly, since I am able to connect directly using establish_connection
method. Is there any documentation on how to specify a odbc connection
in yml for rails?

Here is the list of installed gems ::
D:\work\rails\job>gem list

*** LOCAL GEMS ***

actionmailer (2.3.2, 2.0.0)
actionpack (2.3.2, 2.0.0)
activerecord (2.3.2, 2.0.0)
activerecord-sqlserver-adapter (2.2.18)
activeresource (2.3.2)
activesupport (2.3.2, 2.0.0)
dbd-odbc (0.2.4)
dbi (0.4.1)
deprecated (2.0.1)
fxri (0.3.7, 0.3.6)
fxruby (1.6.19, 1.6.12)
hpricot (0.8.1, 0.6)
log4r (1.0.5)
ptools (1.1.6)
rails (2.3.2)
rake (0.8.7, 0.7.3)
sources (0.0.1)
test-unit (2.0.2)
win32-api (1.4.2, 1.0.4)
win32-clipboard (0.5.1, 0.4.3)
win32-dir (0.3.4, 0.3.2)
win32-eventlog (0.5.0, 0.4.6)
win32-file (0.6.1, 0.5.4)
win32-file-stat (1.3.3, 1.2.7)
win32-process (0.6.0, 0.5.3)
win32-sapi (0.1.4)
win32-sound (0.4.1)
windows-api (0.3.0, 0.2.0)
windows-pr (1.0.5, 0.7.2)

Thanks and appreciate any help leading to the resolution.
--
Posted via http://www.ruby-forum.com/.

Frederick Cheung

unread,
Jun 8, 2009, 12:14:51 PM6/8/09
to Ruby on Rails: Talk
On Jun 8, 4:23 pm, Steve Dc <rails-mailing-l...@andreas-s.net> wrote:
> New to rails. Installed rails on my XP desktop, and setup my first app
> that will use an existing SQLServer database. I am able to connect to
> SQLServer using odbc with following code.
>
> require 'rubygems'
> require 'ActiveRecord'
>
> ActiveRecord::Base.establish_connection(
>   :adapter  => 'sqlserver',
>   :mode => 'odbc',
>   :dsn => 'Driver={SQL Server};Server=<server>;Database=<db>',
>   :username => 'uid',
>   :password => 'pwd'
> )
>
> class src <ActiveRecord::Base
>   set_table_name 'Source'
>   set_primary_key 'IDSource'
> end
>
> s = []
> s = Source.find(1)
> puts s.SourceName
>
> I was able to connect and the code behaves as expected. However when I
> try to use database.yml to specify the same connection information, I am
> unable to connect using script/dbconsole.

script/dbconsole is just a handy shortcut for opening a connection to
the database using whatever command line tool that database provides
(eg with mysql it just runs the mysql utility). What rails is telling
you here is that it doesn't know what it should launch for this kind
of database (which isn't entirely surprising what with the sqlserver
being unbundled from rails). This isn't actually related to the
ability to connect to the database from a rails app though.

Fred

Steve Dc

unread,
Jun 8, 2009, 2:21:39 PM6/8/09
to rubyonra...@googlegroups.com
Frederick Cheung wrote:
> On Jun 8, 4:23�pm, Steve Dc <rails-mailing-l...@andreas-s.net> wrote:
>> � :dsn => 'Driver={SQL Server};Server=<server>;Database=<db>',

>> s = Source.find(1)
>> puts s.SourceName
>>
>> I was able to connect and the code behaves as expected. However when I
>> try to use database.yml to specify the same connection information, I am
>> unable to connect using script/dbconsole.
>
> script/dbconsole is just a handy shortcut for opening a connection to
> the database using whatever command line tool that database provides
> (eg with mysql it just runs the mysql utility). What rails is telling
> you here is that it doesn't know what it should launch for this kind
> of database (which isn't entirely surprising what with the sqlserver
> being unbundled from rails). This isn't actually related to the
> ability to connect to the database from a rails app though.
>
> Fred

Thanks Fred. I will ignore this issue and move on. But next this is
where I am stuck.

I have the following code in app/controllers/processjobs_controller.rb
#
class ProcessJobsController <ApplicationController
def index
@ProcessJobs = ProcessJobs.find(:all)
end
end
#

This is my route file ..
ActionController::Routing::Routes.draw do |map|
map.resources :ProcessJobs

map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
end

When I point my browser to localhost:3000/ProcessJobs, this is what I
see in the log file. When I see the first few lines, it appears that
rails is trying to logon as 'sa'. The database.yml file has the
following contents.

development:
adapter : sqlserver
mode : odbc
dsn : Driver={SQL Server};Server=<server>

database : ODS
username : <definitely_not_sa>
password : <pwd>

The log file also shows this :
NameError (uninitialized constant ProcessJobsController)

Not sure how to interpret this message.

The contents of the log file follows:

/!\ FAILSAFE /!\ Mon Jun 08 08:42:58 -0500 2009
Status: 500 Internal Server Error
28000 (18456) [Microsoft][ODBC SQL Server Driver][SQL Server]Login
failed for user 'sa'.
d:/ruby/lib/ruby/gems/1.8/gems/dbd-odbc-0.2.4/lib/dbd/odbc/driver.rb:36:in
`connect'
d:/ruby/lib/ruby/gems/1.8/gems/dbi-0.4.1/lib/dbi/handles/driver.rb:33:in
`connect'
d:/ruby/lib/ruby/gems/1.8/gems/dbi-0.4.1/lib/dbi.rb:142:in `connect'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-sqlserver-adapter-2.2.18/lib/active_record/connection_adapters/sqlserver_adapter.rb:775:in
`connect'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-sqlserver-adapter-2.2.18/lib/active_record/connection_adapters/sqlserver_adapter.rb:211:in
`initialize'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-sqlserver-adapter-2.2.18/lib/active_record/connection_adapters/sqlserver_adapter.rb:26:in
`new'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-sqlserver-adapter-2.2.18/lib/active_record/connection_adapters/sqlserver_adapter.rb:26:in
`sqlserver_connection'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:223:in
`send'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:223:in
`new_connection'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:245:in
`checkout_new_connection'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:188:in
`checkout'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:184:in
`loop'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:184:in
`checkout'
d:/ruby/lib/ruby/1.8/monitor.rb:242:in `synchronize'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:183:in
`checkout'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:98:in
`connection'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:326:in
`retrieve_connection'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:123:in
`retrieve_connection'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_specification.rb:115:in
`connection'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/query_cache.rb:9:in
`cache'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/query_cache.rb:28:in
`call'
d:/ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:361:in
`call'
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/head.rb:9:in
`call'
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/methodoverride.rb:24:in
`call'
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/params_parser.rb:15:in
`call'
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/rewindable_input.rb:25:in
`call'
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/session/cookie_store.rb:93:in
`call'
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/reloader.rb:9:in
`call'
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/failsafe.rb:11:in
`call'
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/lock.rb:11:in
`call'
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/lock.rb:11:in
`synchronize'
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/lock.rb:11:in
`call'
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/dispatcher.rb:106:in
`call'
d:/ruby/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/rack/static.rb:31:in
`call'
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/urlmap.rb:46:in
`call'
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/urlmap.rb:40:in
`each'
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/urlmap.rb:40:in
`call'
d:/ruby/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/rails/rack/log_tailer.rb:17:in
`call'
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/content_length.rb:13:in
`call'
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/handler/webrick.rb:46:in
`service'
d:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
d:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
d:/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
d:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
d:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
d:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
d:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
d:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
d:/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
d:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'
d:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.2/lib/action_controller/vendor/rack-1.0/rack/handler/webrick.rb:13:in
`run'
d:/ruby/lib/ruby/gems/1.8/gems/rails-2.3.2/lib/commands/server.rb:111
d:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`gem_original_require'
d:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
`require'
script/server:3


Processing ActionController::Base#index (for 127.0.0.1 at 2009-06-08
13:04:12) [GET]

NameError (uninitialized constant ProcessJobsController):
d:/ruby/lib/ruby/1.8/webrick/httpserver.rb:104:in `service'
d:/ruby/lib/ruby/1.8/webrick/httpserver.rb:65:in `run'
d:/ruby/lib/ruby/1.8/webrick/server.rb:173:in `start_thread'
d:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start'
d:/ruby/lib/ruby/1.8/webrick/server.rb:162:in `start_thread'
d:/ruby/lib/ruby/1.8/webrick/server.rb:95:in `start'
d:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `each'
d:/ruby/lib/ruby/1.8/webrick/server.rb:92:in `start'
d:/ruby/lib/ruby/1.8/webrick/server.rb:23:in `start'
d:/ruby/lib/ruby/1.8/webrick/server.rb:82:in `start'

Rendered rescues/_trace (109.0ms)
Rendered rescues/_request_and_response (0.0ms)
Rendering rescues/layout (internal_server_error)

Richard Lenawasae

unread,
Oct 4, 2014, 3:35:54 AM10/4/14
to rubyonra...@googlegroups.com
Email Address:richardl...@gmail.com

Hi Guys,

I'm still new on Ruby on Rails and am now developing a real world
project, i have an existing sql server database, but am getting problem
when i try to migrate schema.rb without scaffolding since my tables has
big dozen of columns....

Please help..

Scott Ribe

unread,
Oct 4, 2014, 8:41:56 AM10/4/14
to rubyonra...@googlegroups.com, Richard Lenawasae

On Oct 4, 2014, at 1:31 AM, Richard Lenawasae <li...@ruby-forum.com> wrote:

> an existing sql server database, but am getting problem
> when i try to migrate...

Don't do that ;-) Migrations are an optional feature, designed to manage evolution of schemas created and maintained by Rails. It's perfectly possible to create and maintain your schema as you always have, and use Rails to access it.


--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
(303) 722-0567 voice




Richard Lenawasae

unread,
Oct 4, 2014, 9:14:44 AM10/4/14
to rubyonra...@googlegroups.com
So can i start scaffolding manually from ruby command prompt, but i know
scaffolding manually is quite tiresome and if i can really get another
alternative then fine...?

Scott Ribe

unread,
Oct 4, 2014, 9:31:13 AM10/4/14
to rubyonra...@googlegroups.com, Richard Lenawasae
On Oct 4, 2014, at 7:12 AM, Richard Lenawasae <li...@ruby-forum.com> wrote:

> So can i start scaffolding manually from ruby command prompt, but i know
> scaffolding manually is quite tiresome and if i can really get another
> alternative then fine...?

Do you really need scaffolding at all?

Ganesh Ranganathan

unread,
Oct 4, 2014, 2:15:22 PM10/4/14
to rubyonra...@googlegroups.com

On Sat, Oct 4, 2014 at 6:42 PM, Richard Lenawasae <li...@ruby-forum.com> wrote:
So can i start scaffolding manually from ruby command prompt, but i know
scaffolding manually is quite tiresome and if i can really get another
alternative then fine...?

​Why not use the ActiveRecord Sql Server driver and rails console?​

Jason Fleetwood-Boldt

unread,
Oct 6, 2014, 10:53:19 AM10/6/14
to rubyonra...@googlegroups.com


As discussed last week, generally Rails scaffolding is not commonly used.

Database migrations are commonly used, but it is unclear to me what you are trying to do.

Are you attaching a Rails app to an existing database? (Like a database schema that is already built and works off some other application?) Migrations are designed to build out your database schema bit-by-bit (migration-by-migration), so you'll have to get creative with how to work around that. 

If you're simply having problems running a migration (rake db:migrate), paste the full text of the error message you are seeing. 
-- 
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4a6883e271880e5dee3dea330a78bd66%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.


----

Jason Fleetwood-Boldt
te...@datatravels.com
http://www.jasonfleetwoodboldt.com/writing

All material © Jason Fleetwood-Boldt 2014. Public conversations may be turned into blog posts (original poster information will be made anonymous). Email ja...@datatravels.com with questions/concerns about this.

Richard Lenawasae

unread,
Oct 10, 2014, 12:36:15 AM10/10/14
to rubyonra...@googlegroups.com
Hi,

Please help here....When i try to migrate database "{name}" it shows an
error says!

ActiveRecord::DuplicateMigrationNameError:.......

("Multiple migrations have the name #{name}") and i'm sure i was done
with this table #{name}") previously..

surely it cannot migrate.

Vivek Sampara

unread,
Oct 10, 2014, 5:29:59 AM10/10/14
to rubyonra...@googlegroups.com
DuplicateMigrationNameError Basically means that there is a class already defined with the same name

db/migrate/sometimestamp_your_migration_name.rb

class YourMigrationName < ActiveRecord::Migration

end

So give the migration file a different name and change the class name as well along with it. something like 

db/migrate/sometimestamp_your_migration_name2.rb

class YourMigrationName2 < ActiveRecord::Migration

end



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.

Richard Lenawasae

unread,
Oct 10, 2014, 1:45:02 PM10/10/14
to rubyonra...@googlegroups.com
Thank You Vivek Sampara, but actually i have found i didn't save the
previous migration and it duplicate itself..
Reply all
Reply to author
Forward
0 new messages