On 21 December 2011 14:47, Michael M. wrote:
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

class Sop10200 < ActiveRecord::Base
self.establish_connection :gpconn
set_table_name 'SOP10200'

def self.invoice_items(invoicenmbr)
where('SOP10200.SOPNUMBE = ?', invoicenmbr)


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.
I guess the problem is that the select statement returns a collection
of Sop10200 objects, so there is nowhere to put the data from the
other table. Normally in rails one would have relationships between
the tables so that the joined data has somewhere to go.

find_by_sql might help, have a look at http://www.railsrocket.com/find_by_sql.


You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

Discussion Posts


Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 4 of 4 | next ›
Discussion Overview
grouprubyonrails-talk @
postedDec 21, '11 at 2:47p
activeDec 21, '11 at 4:18p



site design / logo © 2021 Grokbase