{"action":"closed","issue":{"url":"https://api.github.com/repos/ankane/multiverse/issues/22","repository_url":"https://api.github.com/repos/ankane/multiverse","labels_url":"https://api.github.com/repos/ankane/multiverse/issues/22/labels{/name}","comments_url":"https://api.github.com/repos/ankane/multiverse/issues/22/comments","events_url":"https://api.github.com/repos/ankane/multiverse/issues/22/events","html_url":"https://github.com/ankane/multiverse/issues/22","id":376860110,"node_id":"MDU6SXNzdWUzNzY4NjAxMTA=","number":22,"title":"It is possible manually change DB connection with multiverse?","user":{"login":"creadone","id":990803,"node_id":"MDQ6VXNlcjk5MDgwMw==","avatar_url":"https://avatars1.githubusercontent.com/u/990803?v=4","gravatar_id":"","url":"https://api.github.com/users/creadone","html_url":"https://github.com/creadone","followers_url":"https://api.github.com/users/creadone/followers","following_url":"https://api.github.com/users/creadone/following{/other_user}","gists_url":"https://api.github.com/users/creadone/gists{/gist_id}","starred_url":"https://api.github.com/users/creadone/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/creadone/subscriptions","organizations_url":"https://api.github.com/users/creadone/orgs","repos_url":"https://api.github.com/users/creadone/repos","events_url":"https://api.github.com/users/creadone/events{/privacy}","received_events_url":"https://api.github.com/users/creadone/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-11-02T15:34:57Z","updated_at":"2018-11-06T09:32:42Z","closed_at":"2018-11-06T09:32:42Z","author_association":"NONE","body":"Hi @ankane, thanks for elegant solution!\r\n\r\nI have two databases that work with multiverse. All of models linked with first DB, but some necessary data stored in the second DB. I need make very complex SQL requests to second DB. I can't write this requests as model methods because it make my models literally dirty. Then i wrap requests code into `app/queries/query_...rb` like this:\r\n\r\n```Ruby\r\nclass SomeReport << Report\r\n  attr_accessor :query\r\n  \r\n  def initialize(query)\r\n    ActiveRecord::Base.establish_connection :\"second_db_#{Rails.env}\"\r\n    @connection = ActiveRecord::Base.connection\r\n    @query = query.downcase\r\n  end\r\n\r\n  def build_request\r\n    [... giant SQL from my DBA with @query ...]\r\n  end\r\n\r\n  def execute_statement(sql)\r\n    result = @connection.execute(sql).to_a\r\n    if result.present?\r\n      return result\r\n    else\r\n      return []\r\n     end\r\n  end\r\n\r\n  def call\r\n     sql = build_request\r\n     result = execute_statement(sql)\r\n   end\r\n```\r\nNext, in the controller i call `SomeReport` and get data but after it Rails change connection to from first to second DB and all of my models which were use connection for first DB falling because not found tables from first DB in the second.\r\n\r\nOf course, after call `SomeReport` i can do reconnect to first DB but maybe i will can do it more simple with multiverse? How to use multiverse not in models?"}}
{"actor":{"display_login":"creadone"}}