title: Using Active Record Base Connection to Pull data from Multiple Servers
body: <p>I have a small Rails 4.2 app which is basically a glorified metrics app.  I want to be able to create metric records based off of a few class methods.</p>
  
  <p>The problem is, I need to pull the data from multiple production servers into this one server and I'm not sure on how to do that using AR Base Connection.</p>
  
  <p>Here's my target (metric server) methods in the Metric Class:</p>
  
  <pre><code>class Metric &lt; ActiveRecord::Base
  
    def self.last_year_mtd
      m = Metric.new
      m.agency_name = Figaro.env.agency_name
      m.metric_type = "Last Year MTD"
      sql = ActiveRecord::Base.connection.execute("select sum(dst_amt::DECIMAL) from distributions where dst_code like 'COL%' and dst_posted_date::DATE BETWEEN (select (date_trunc('month', current_date)) - INTERVAL '13 Month') and (select (date_trunc('month', current_date)) - INTERVAL '366 Day')")
      total = sql[0]["sum"].to_s
      m.amount = total
      m.save
    end
  
    def self.last_month
      m = Metric.new
      m.agency_name = Figaro.env.agency_name
      m.metric_type = "Last Month"
      sql = ActiveRecord::Base.connection.execute("select sum(dst_amt::DECIMAL) from distributions where dst_code like 'COL%' and dst_posted_date::DATE BETWEEN (select (date_trunc('month', current_date)) - INTERVAL '1 Month') and (select date_trunc('month', now())::date - 1)")
      total = sql[0]["sum"].to_s
      m.amount = total
      m.save
    end
  
    def self.current_month
      m = Metric.new
      m.agency_name = Figaro.env.agency_name
      m.metric_type = "Current Month"
      sql = ActiveRecord::Base.connection.execute("select sum(dst_amt::DECIMAL) from distributions where dst_code like 'COL%' and dst_posted_date::DATE BETWEEN (select (date_trunc('month', current_date))) and NOW()::DATE")
      total = sql[0]["sum"].to_s
      m.amount = total
      m.save
    end
  end
  </code></pre>
  
  <p>So when I have built this class and tested it in a source server (one I'm trying to pull metrics from) all of these methods work properly and create the metrics since I'm pulling from the local database.</p>
  
  <p>The problem is I have like 8 production servers to pull this data from, execute the SQL on, and create records from.</p>
  
  <p>How would I go about doing this on the metric server.  I'm assuming I'll need to setup all 8 server's postgres info in database.yml and name them each something unique (like acme, johnson, joe, etc).  But in my class and methods, how do I call out to all 8 servers using a block and iterated over each one creating a new metric record for each server and storing it to the local metric model/database?</p>
  
  <p>I'm vaguely familiar with AR abstract so I'm not sure if this is what I need or if there's a simpler way to do it that I'm not seeing in my research.</p>
  
  <p>Thanks in advance for any advice you can provide.</p>