function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jchanowitzjchanowitz 

Self Joins using ActiveSalesforce

Hi,

 

I'm trying to access a SFDC database from Rails, that has a custom object Employee__c, which in turn has a self referential foreign key Supervisor__r, pointing back to Employee__c.  In Rails, I have a model Employee, which has a set_table_name of 'Employee__c'.  Does anyone know how I can get the supervisor's name on a query to the employee.  It doesn't seem as if Rails allows a model to associate to itself.  Also, if I try just to pass the soql directly by using Employee.find_by_sql(), I can't figure out how to label the Supervisor__r foreign key in my query (which is being changed to supervisor_id__c when returned by asf).  For example:

 

Employee.find_by_sql("select e.id, e.name, e.Supervisor__r.name from Employee__c") returns

 

NoMethodError: You have a nil object when you didn't expect it!

The error occurred while evaluating nil.name

from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-activesalesforce-adapter-2.0.0/lib/active_record/connection_adapters/activesalesforce_adapter.rb:353:in `add_rows'

from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-activesalesforce-adapter-2.0.0/lib/active_record/connection_adapters/activesalesforce_adapter.rb:347:in `each'from

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-activesalesforce-adapter-2.0.0/lib/active_record/connection_adapters/activesalesforce_adapter.rb:347:in `add_rows'

from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-activesalesforce-adapter-2.0.0/lib/active_record/connection_adapters/activesalesforce_adapter.rb:344:in `each'from

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-activesalesforce-adapter-2.0.0/lib/active_record/connection_adapters/activesalesforce_adapter.rb:344:in `add_rows'

from C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-activesalesforce-adapter-2.0.0/lib/active_record/connection_adapters/activesalesforce_adapter.rb:327:in `select_all'from

C:/Ruby/lib/ruby/gems/1.8/gems/activerecord-2.3.3/lib/active_record/base.rb:661:in `find_by_sql'

from (irb):10

 

Has anybody done this?

 

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
jchanowitzjchanowitz

Thanks for the pointer.  I'll look over there.

 

 

All Answers

apexsutherlandapexsutherland

Hi jchanowitz,

 

I think that your best bet for getting this question answered is on the ActiveSalesforce Google Group:

 

http://groups.google.com/group/activesalesforce

 

We have most of the users and developers of ActiveSalesforce on that mailing list and someone there should be able to help you.

 

 

- Alex

jchanowitzjchanowitz

Thanks for the pointer.  I'll look over there.

 

 

This was selected as the best answer