Search code examples
ruby-on-railsrubygemsdatabasedotcom-gem

Ruby Gem "Databasedotcom" same object issue


I am using the Ruby Gem "Databasedotcom" to integrate Salesforce in a Rails app and all works fine, but I now facing a problem with the same object name in the Rails application and Salesforce.

I have a User model in my Rails application and User object is also in Salesforce. So when trying to get data from Salesforce User object it always return the Rails application User object.

Here is my code

client = Databasedotcom::Client.new
client.client_id      #=> foo
client.client_secret  #=> bar

client.authenticate :username => "[email protected]", :password => "ThePasswordTheSecurityToken"  #=> "the-oauth-token"

client.materialize("User");

@user = User.find_by_Id('005d0000001291x');

but above statement always use the User model from Rails.

I want to use the Salesforce User object to get its data.

Thanks for any help.


Solution

  • At first wrap up you SF code in a module like this:

    module SF
      module Connection          
        def self.client
          @client = Databasedotcom::Client.new("config/salesforce.yml")
          @client.authenticate :username => SF_CONFIG['username'], :password => SF_CONFIG['password_with_token']
          @client
        end
      end
    end
    

    And create initializer where you specify module to use with sobject_module param:

    config/salesforce.yml:

    username: username
    password_with_token: token
    client_secret: secret
    client_id: cliend_id
    sobject_module : SF
    

    Add initializer to config/initializers:

    salesforce.rb:

    SF_CONFIG = YAML.load_file('config/salesforce.yml') rescue nil
    

    Now you can use User inside SF module and it will be SF User, not you AR one.

    And also I highly recommend you to read the documentation, there are quite a lot methods that will help you make your SF code cleaner: http://rubydoc.info/github/heroku/databasedotcom/master/frames

    Good luck with SF!