I have a Rails application that is set up to use a MySQL database for
most of its data but I have a need to pull in data from a SQL Server
2008 instance as well. This holds our data for Microsoft’s CRM and GP
and therefore cannot be pulled into the MySQL database. So I set up a
database configuration in config/database.yml for the connection to this
server and the connection seems to work just fine.
With that said, I have a couple of queries that I need to execute on
this database which joins together two tables and pulls columns from
each table. So I set up a model file for one of the tables like the
following…
class Sop10200 < ActiveRecord::Base
self.establish_connection :gpconn
set_table_name ‘SOP10200’
def self.invoice_items(invoicenmbr)
select(‘RGI_ENTLMT,RGI_LICLTH,RGI_PRVENTLMT,ITEMNMBR,QUANTITY,ITEMDESC’).
joins(‘INNER JOIN RGI10200 ON (RGI10200.SOPNUMBE=SOP10200.SOPNUMBE
AND RGI10200.SOPTYPE=SOP10200.SOPTYPE AND
RGI10200.LNITMSEQ=SOP10200.LNITMSEQ)’).
where(‘SOP10200.SOPNUMBE = ?’, invoicenmbr)
end
end
The first three columns in the select statement come from table RGI10200
and the last three come from SOP10200. The query executes and returns
without any errors, but when I do an inspect on the results that come
back, I realized that the tables from RGI10200 were missing. If I were
to switch them and create the model class around RGI10200, then the
fields from SOP10200 are missing. Any idea why I am missing fields from
the results?
To make sure it was sending the correct query, I copied the query that
gets put into the development log and used that in a DB client connected
to the server with the same credentials and it comes back with all the
fields present. I also verified using a packet capture that the data is
in fact coming back to the Rails system with all of the columns
populated. So it isn’t the query formation or the DB server giving me
back the data I ask for.
Thanks in advance,
Michael