views:

307

answers:

0

Hey guys, my first question here.

I am making a site in RoR and I am inside a multi DB environement. By that, I mean that some of my models are linked to MSSQL tables, and some others are linked to MYSQL tables.

It works well for the most part, but when I use the 'include' option in a find method, I get a very weird mix of SQL. Let me show you an example:

[SELECT * FROM "viewInfoClient" WHERE ("viewInfoClient".`NoClient` IN (6044196,5000652,0204392)) ]

MSSQL uses " between tables and columns name

MYSQL uses `

When I use the :include option in a MYSQL model, it will try and go read the corresponding results in the MSSQL model table. Since the NoClient link field comes from my MYSQL model, it get mixed and MSSQL trows an error wich is logic.

[unixODBC][FreeTDS][SQL Server]Incorrect syntax near '`'

Any idea how I can solve this problem?

Thanks

CLIENT MODEL (MSSQL Database)

class Client < ActiveRecord::Base  
  establish_connection "mssql_#{RAILS_ENV}"  
  set_table_name "viewInfoClient"  
  set_primary_key "NoClient"  
  has_many :billets, :foreign_key => 'noclient', :primary_key => 'NoClient'
end

BILLET MODEL (MySQL Database)

class Billet < ActiveRecord::Base
  belongs_to :client, :foreign_key => 'noclient'
end

AFFECTED STATEMENT Could be anything using :include in it between the 2. Example from the Billet model

def findall
  find(:all, :include => 'client', :conditions => 'bin_id = 1')
end

Will return:

SELECT * FROM "viewInfoClient" WHERE ("viewInfoClient".`NoClient` IN (6044196,5000652,0204392))

Where 6044196,5000652,0204392 are the 3 records that have bin_id = 1 in the Billet model.

I remove everything else from my models to shorten the code, but this is it basically. I can reproduce it from any model that use a MySQL - MSSQL link.