Search code examples
mysqlruby-on-railsdatabasesalesforcedatabasedotcom-gem

RoR Use 2 different databases with active record adapter? same tablenames


Currently I am developing an rails project to connect the database from salesforce.com with our SQL server. Atm we use the gems 'mysql2' for the sql part and 'databasedotcom' + 'databasedotcom-rails' for the salesforce part.

Our problem ist, that we have the same names for the tables in both databases. So both adapter try to encapsulate them into activerecord classes and as you guess, its a big problem. Unfortunately we found no solution in the internet. So how can I prevent the namespaceconfict?


Solution

  • To solve the problem with the conflicting namespaces, we put the module for mysql in his own namespace. the databasedotcom gem create the models on runtime, so you just have to create models for your mysql db.

    create modeles for sql
    create folder named after your wished prefix in models folder (for us: Mad)
    put your models in the namespacee-folder (for us: Mad)
    wrap the model classes in module <namespace>
    

    like account.rb:

    module Mad
        class Account < ActiveRecord::Base
            attr_accessible :name, :website, :phone
        end
    end
    

    now you can access the Accounts for salesforce via Account and the mysql with Mad::Account