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.
establish_connection- S.Ivanovdatabase.ymland called, as you indicated. But in this application there is a tabletokens, where user tokens are added (Redmine application), respectively connecting to a remote database, it falls out with an error: - S.IvanovActiveRecord::StatementInvalid (Mysql2::Error: Table 'amethyst.tokens' doesn't exist: UPDATEtokens` SETtokens.updated_on= '2016-10-18 10:00:38' WHEREtokens.user_id= 2921 ANDtokens.value= 'fbb2e4ece07' ANDtokens.action= 'session'): app / models / user.rb: 414: inverify_session_token'. I can't create any tables there, I just need to take data from there - S.IvanovActiveRecord::StatementInvalid (Mysql2::Error: Table 'amethyst.tokens' doesn't exist: UPDATEtokens` SETtokens.updated_on= '2016-10-18 10:00:38' WHEREtokens.user_id= 2921 ANDtokens.value= 'fbb2e4ece07e76e9f5605ffb9549607a37b51b1a' ANDtokens.action= 'session'): app / models / user.rb: 414: inverify_session_token' app/controllers/application_controller.rb:77:in`. Occurs when trying to collect data from an external database - S.Ivanov