The link has already asked a question. Everything turned out, but. I register the connection parameters to the external database:

class AmethystModel < ActiveRecord::Base @amethyst_conn = ActiveRecord::Base.establish_connection ( { :adapter => 'mysql2', :database => 'RemouteDB', :host => 'examle.remoutehost.com', :username => 'root', :password => "root" } ) end 

Then I try to use this connection:

 def self.get_all_from_amethyst sql = sanitize_sql(["SELECT * from RemouteDB.param_delay order by sequence"]) @all_amethyst = @amethyst_conn.connection.execute(sql) end 

But, the problem is that he still tries to execute this query using the database specified in database.yml . Accordingly, it gives an error:

 ActionView::Template::Error (Mysql2::Error: Table 'RemouteDB.param_delay' doesn't exist: SELECT MAX(sequence) as max from RemouteDB.param_delay) 

Why are connection parameters that are committed to ActiveRecord::Base.establish_connection not perceived? I need to set the connection parameters in this way, without using the database.yml application.

UPD: There is a ready-made Rails application (Redmine), which has a number of models (Issue, Project, User, Token, etc). I need to add my logic there. There is some external base from which I need to get data. This database is not related to the application database. So, I created a model:

 class AmethystModel < ActiveRecord::Base self.abstract_class = true @amethyst_conn = ActiveRecord::Base.establish_connection :external def self.get_all_from_amethyst sql = sanitize_sql(["SELECT * from Amethyst.param_delay order by sequence"]) @all_amethyst = @amethyst_conn.connection.execute(sql) end end 

external parameters described in database.yml . Where I described a number of methods for working with data from this table. I also created a view and controller to work with this model. However, after these actions the whole application stopped working, an error is displayed:

 Processing by WelcomeController#index as HTML Completed 500 Internal Server Error in 4ms (ActiveRecord: 2.8ms) ActiveRecord::StatementInvalid (Mysql2::Error: Table 'amethyst.tokens' doesn't exist: UPDATE `tokens` SET `tokens`.`updated_on` = '2016-10-18 14:30:29' WHERE `tokens`.`user_id` = 2921 AND `tokens`.`value` = 'fbb2e4ece07e76e9f5605ffb9549607a37b51b1a' AND `tokens`.`action` = 'session'): app/models/user.rb:414:in `verify_session_token' app/controllers/application_controller.rb:77:in `session_expired?' app/controllers/application_controller.rb:67:in `session_expiration' config/initializers/ntlm-sso.rb:31:in `call' 

I suppose because there is no tokens table in the external database, but it shouldn’t be there, it has nothing to do with the application, I just need to get data from there. In PHP, it was written simply:

  $redmine_conn = new mysqli("examle.remoutehost.com", "root", "root", "RemouteDB"); 

This was enough; this connection did not contact the current one that the application uses.

  • You are doing somewhat not what I wrote in your answer. - D-side
  • @ D-side, lda, you suggested to register in database.yml, but I want to simply register connect to this database, without using database.yml, take the data and everything, I don’t need anything else from it. I have indicated how, according to the documentation, the connection parameters are in the form of a hash right in the establish_connection - S.Ivanov
  • @D-side, I did, added a new entry to database.yml and called, as you indicated. But in this application there is a table tokens , where user tokens are added (Redmine application), respectively connecting to a remote database, it falls out with an error: - S.Ivanov
  • ActiveRecord::StatementInvalid (Mysql2::Error: Table 'amethyst.tokens' doesn't exist: UPDATE tokens` SET tokens . updated_on = '2016-10-18 10:00:38' WHERE tokens . user_id = 2921 AND tokens . value = 'fbb2e4ece07' AND tokens . action = 'session'): app / models / user.rb: 414: in verify_session_token' . I can't create any tables there, I just need to take data from there - S.Ivanov
  • @ D-side, the error is: ActiveRecord::StatementInvalid (Mysql2::Error: Table 'amethyst.tokens' doesn't exist: UPDATE tokens` SET tokens . updated_on = '2016-10-18 10:00:38' WHERE tokens . user_id = 2921 AND tokens . value = 'fbb2e4ece07e76e9f5605ffb9549607a37b51b1a' AND tokens . action = 'session'): app / models / user.rb: 414: in verify_session_token' app/controllers/application_controller.rb:77:in `. Occurs when trying to collect data from an external database - S.Ivanov

0