Jump to content

The ultimate community for Ruby on Rails developers.


Photo

The proper way to connect to a remote db in rails 4


  • Please log in to reply
3 replies to this topic

#1 Vell

Vell

    Inspector

  • Members
  • 78 posts
  • LocationWashington, DC

Posted 23 June 2014 - 07:47 PM

Hello all,

 

I am attempting to connect to a remote MSSQL server to use an authentication server and I am finding myself lost on the best way to finish the implementation.

 

I have added a section for the MSSQL server in my database.yml file but from there I am not sure the best way to proceed. The original way that I learned how to do this was to create model that establishes a connecting to the remote server and use that as my go between using ActiveRecord.

 

I was told at some point this was not the best approach. I am curious what the proper way is.

 

Ideally the end result of this is that I covert this into a gem to be used with my other rails apps but I have never created a gem before. But first thing is first, implementing it correctly from within my app.

 

Any thoughts, feedback, or references would be greatly appreciated.

 

Thanks,



#2 Ohm

Ohm

    Driver

  • Moderators
  • 469 posts
  • LocationCopenhagen

Posted 23 June 2014 - 07:59 PM

You can use the establish_connetion method in ActiveRecord to connect to multiple databases. (http://apidock.com/r...onnection/class)

 

As GavinLaking is saying on the above linked page, in the models that need this other database, you can just call

establish_connection @connection_object

Blog: http://ohm.sh | Twitter: @madsohm | Work: Lokalebasen.dk


#3 Vell

Vell

    Inspector

  • Members
  • 78 posts
  • LocationWashington, DC

Posted 24 June 2014 - 01:46 PM

It looks like I am close. It looks like I am getting a ,for lack of better terminology, translation problem. Where I have called my class person and the query is searching for a table called people when the table is actually called person. I am not sure how to address that.

 

example:

Person.first

returns the following:

Person Load (1.7ms)  EXEC sp_executesql N'SELECT TOP (1) [people].* FROM [people] ORDER BY [people].[id] ASC'
TinyTds::Error: Invalid object name 'people'.: EXEC sp_executesql N'SELECT TOP (1) [people].* FROM [people] ORDER BY [people].[id] ASC'
ActiveRecord::StatementInvalid: TinyTds::Error: Invalid object name 'people'.: EXEC sp_executesql N'SELECT TOP (1) [people].* FROM [people] ORDER BY [people].[id] ASC'
from /Users/lovell/.rvm/gems/ruby-2.0.0-p451/bundler/gems/activerecord-sqlserver-adapter-34d5c731059b/lib/active_record/connection_adapters/sqlserver/database_statements.rb:423:in `each' 

So far my set up looks like so:

 

$HOME/.freetds.conf

[sqlserver]
  host =  server.domain
  port = 1433
  tds version = 7.0
  database = db_name

config/database.yml

sqlserver_development:
  adapter: sqlserver
  host: server.domain
  mode: :dblib
  database: db_name
  username: username
  password: password

models/sql_server.rb

class SqlServer < ActiveRecord::Base
  establish_connection :sqlserver_development
  self.abstract_class = true
end

models/person.rb

class Person < SqlServer
end


#4 Vell

Vell

    Inspector

  • Members
  • 78 posts
  • LocationWashington, DC

Posted 24 June 2014 - 01:55 PM   Best Answer

Ah I found it. Setting self.table_table = "table_name" works. I was able to do Person.first and get a result back. Painful but not as painful as I thought it would be.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users