title: Writing a SQL Server query in a Rails controller
body: <p>I am trying to execute a raw SQL query in my rails controller. I tested the query a database program and it works. </p>
  
  <p>Here is my controller (sites_controller.rb):</p>
  
  <pre><code>class SitesController &lt; ApplicationController
    def index
      @sites = SELECT * FROM "AM-Martin".dbo.CpCore_Site WHERE "Number" LIKE 'LA%' OR "Number" LIKE 'LC%'
      @hash = Gmaps4rails.build_markers(@sites) do |site, marker|
        marker.lat site.latitude
        marker.lng site.longitude
      end
    end
  end
  </code></pre>
  
  <p>I keep getting this long error message that it is not liking the way I wrote it. </p>
  
  <p>Can anyone help guide me please?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>My controller has changed to:</p>
  
  <pre><code>class SitesController &lt; ApplicationController
    def index
        query = "SELECT * FROM dbo.CpCore_Site WHERE 'Number' LIKE 'LA%' OR 'Number' LIKE 'LC%'"
        @sites = ActiveRecord::Base.connection.execute(query)
  
        @hash = Gmaps4rails.build_markers(@sites) do |site, marker|
        marker.lat site.latitude
        marker.lng site.longitude
  
      end
    end
  end
  </code></pre>
  
  <p>But now I am getting an error for the site.latitude part which says:</p>
  
  <blockquote>
    <p>undefined method `latitude' for 0:Fixnum</p>
  </blockquote>
  
  <p>latitude is one of columns from the database table that I am trying to extract.</p>