4

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?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
ThommyH
  • 310
  • 2
  • 6
  • 15

2 Answers2

2

I may not be understanding the question but I think the following would work for you.

class Account < ActiveRecord::Base
  # table name is 'accounts' in mysql db
end

class SalesForceAccount < ActiveRecord::Base
  establish_connection "#{Rails.env}_salesforce"
  table_name = :accounts
end

With a database.yml of

development:
  # your mysql credentials and adapter info
development_salesforce:
  # your salesforce credentials and adapter info
Kyle
  • 21,978
  • 2
  • 60
  • 61
  • thanks for your answer! how does this solution would solve the problem, that salesforce and mysql setup same-named SomeModel as an activeRecord? maybe i explain the problem a bit more in detail: in both databases are tables with the same name. so we have and account table in both databases. if we use the mentioned gems, both wrap the tables in a class Account < ActiveRecord. So we can use either salseforce or mysql 's Account object. If we do Account.create() we create just for the salesforce DB an object and cant do the same for the mysql database. – ThommyH Aug 06 '12 at 20:32
  • I edited the answer. Does this help? Are you wanting to execute `Account.create(...)` and have that also create a `SalesForceAccount` entry in the db? – Kyle Aug 06 '12 at 20:41
  • ok we may talked at cross purposes :). I already had an connection to both databases, but I want to create account objects for salesforce and mysql independent from each other. i found a good solution by doing it with different namespaces. thanks you anyways! – ThommyH Aug 07 '12 at 11:19
  • My solution does the same as yours except there is no name spacing. To get to the mysql account, use `Account` and to get the the salesforce account, use `SalesForceAccount`. Either way, glad you got it figured out! – Kyle Aug 07 '12 at 12:49
0

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

ThommyH
  • 310
  • 2
  • 6
  • 15