Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About the db_connection option #9

Open
fchampreux opened this issue Mar 4, 2021 · 2 comments
Open

About the db_connection option #9

fchampreux opened this issue Mar 4, 2021 · 2 comments

Comments

@fchampreux
Copy link

Not being higly familiar with connections handling, I am wandering how to specify this option when:

  1. using a connection declared in database.yml config file
  2. using a custom connection to a foreign database

I'll be glad to add examples to the documentation when I solve this point :-)

Thanks a lot!

@sufleR
Copy link
Owner

sufleR commented Mar 6, 2021

@fchampreux I don't know how do you use this gem. Is it ruby on rails application? What rails version?

Rails guides mention about connected_to https://guides.rubyonrails.org/active_record_multiple_databases.html#using-manual-connection-switching
Inside connected_to block, you should be able to

 SqlQuery.new(:get_player_by_email, email: '[email protected]', db_connection: ActiveRecord::Base.connection)

and I think it should work with the connection selected in connected_to. I'm sorry but I didn't test it.

With a custom connection, you should be able to call establish_connection. Docs: https://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/ConnectionHandler.html#method-i-establish_connection

for 1:

 SqlQuery.new(:get_player_by_email, email: '[email protected]', db_connection: ActiveRecord::Base.establish_connection(:development).connection)

for 2:

 SqlQuery.new(:get_player_by_email, email: '[email protected]', db_connection: ActiveRecord::Base.establish_connection({database: '', host: , adapter: .....}).connection)

@fchampreux
Copy link
Author

fchampreux commented Mar 9, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants