I've a Rails 5.2.1 app where each step of a relationship works, but the has_many :through
version doesn't. The setup is a little strange, but I feel like I've set everything up correctly, so I'm a little stumped.
Given this code:
class Contact < SalesforceModel
self.table_name = 'salesforce.contact'
self.primary_key = 'sfid'
has_many :content_accesses, foreign_key: 'contact__c', class_name: 'ContentAccess'
has_many :concepts, through: :content_accesses, source: :inventory
end
class ContentAccess < ApplicationRecord
self.table_name = 'salesforce.content_access__c'
self.primary_key = 'sfid'
belongs_to :inventory, foreign_key: 'inventory__c', inverse_of: :content_accesses, primary_key: 'sfid', class_name: 'Inventory'
belongs_to :contact, foreign_key: 'contact__c', inverse_of: : content_accesses, primary_key: 'sfid', class_name: 'Contact'
end
class Inventory < SalesforceModel
self.table_name = 'salesforce.inventory__c'
self.primary_key = 'sfid'
has_many :content_accesses, foreign_key: 'inventory__c'
has_many :contacts, through: :content_accesses
end
Each step of the has_many :through works:
# Setup
2.5.1 :001 > contact = Contact.first
Contact Load (30.6ms) SELECT "salesforce"."contact".* FROM "salesforce"."contact" ORDER BY "salesforce"."contact"."sfid" ASC LIMIT $1 [["LIMIT", 1]]
=> #<Contact lastname: "Doe", mailingpostalcode: "90210", name: "John Doe", mobilephone: nil, birthdate: nil, phone: nil, mailingstreet: "123 ABC Street", isdeleted: false, systemmodstamp: "2018-03-16 00:09:01", mailingstatecode: "CA", createddate: "2018-03-15 17:50:44", mailingcity: "LA", mailingcountrycode: "US", firstname: "John", email: "[email protected]", sfid: "003m000000txXhwAAE", id: "003m000000txXhwAAE", _hc_lastop: "SYNCED", _hc_err: nil>
# Accessing related ContentAccess works
2.5.1 :002 > contact.content_accesses.count
(2.0ms) SELECT COUNT(*) FROM "salesforce"."content_access__c" WHERE "salesforce"."content_access__c"."contact__c" = $1 [["contact__c", "003m000000txXhwAAE"]]
=> 2
# Accessing related Inventory, through the related ContentAccess works
2.5.1 :003 > contact.content_accesses.first.inventory
ContentAccess Load (0.6ms) SELECT "salesforce"."content_access__c".* FROM "salesforce"."content_access__c" WHERE "salesforce"."content_access__c"."contact__c" = $1 ORDER BY "salesforce"."content_access__c"."sfid" ASC LIMIT $2 [["contact__c", "003m000000txXhwAAE"], ["LIMIT", 1]]
Inventory Load (30.4ms) SELECT "salesforce"."inventory__c".* FROM "salesforce"."inventory__c" WHERE "salesforce"."inventory__c"."sfid" = $1 LIMIT $2 [["sfid", "a1mm0000001S9qzAAC"], ["LIMIT", 1]]
=> #<Inventory createddate: "2018-05-23 15:09:41", isdeleted: false, name: "Some Concept Name", systemmodstamp: "2018-05-23 15:09:42", sfid: "a1mm0000001S9qzAAC", id: "a1mm0000001S9qzAAC", _hc_lastop: "SYNCED", _hc_err: nil>
# Accessing the related inventory through the has_many :through does not work
2.5.1 :004 > contact.concepts.count
(33.0ms) SELECT COUNT(*) FROM "salesforce"."inventory__c" INNER JOIN "salesforce"."content_access__c" ON "salesforce"."inventory__c"."sfid" = "salesforce"."content_access__c"."inventory__c" WHERE "salesforce"."content_access__c"."contact__c" = $1 [["contact__c", "003m000000txXhwAAE"]]
=> 0
Running the generated query in Postgres works, though:
app_development=# SELECT COUNT(*) FROM "salesforce"."inventory__c" INNER JOIN "salesforce"."content_access__c" ON "salesforce"."inventory__c"."sfid" = "salesforce"."content_access__c"."inventory__c" WHERE "salesforce"."content_access__c"."contact__c" = '003m000000txXhwAAE';
count
-------
2
(1 row)
Running Contact.first.concepts.to_sql produces:
SELECT "salesforce"."inventory__c".* FROM "salesforce"."inventory__c" INNER JOIN "salesforce"."content_access__c" ON "salesforce"."inventory__c"."sfid" = "salesforce"."content_access__c"."inventory__c" WHERE "salesforce"."content_access__c"."contact__c" = '003m000000txXhwAAE'
Running that query through psql works fine, returning the proper records from the inventory__c table.
The reverse also has the same problem:
2.5.1 :002 > inventory = Inventory.first
Inventory Load (30.2ms) SELECT "salesforce"."inventory__c".* FROM "salesforce"."inventory__c" ORDER BY "salesforce"."inventory__c"."sfid" ASC LIMIT $1 [["LIMIT", 1]]
=> #<Inventory createddate: "2018-05-23 15:09:41", isdeleted: false, name: "Positive Focus", systemmodstamp: "2018-05-23 15:09:42", inventory_unique_name__c: "Inventory 1", sfid: "a1mm0000001S9qzAAC", id: "a1mm0000001S9qzAAC", _hc_lastop: "SYNCED", _hc_err: nil>
2.5.1 :003 > inventory.content_accesses.first.contact
ContentAccess Load (0.9ms) SELECT "salesforce"."content_access__c".* FROM "salesforce"."content_access__c" WHERE "salesforce"."content_access__c"."inventory__c" = $1 ORDER BY "salesforce"."content_access__c"."sfid" ASC LIMIT $2 [["inventory__c", "a1mm0000001S9qzAAC"], ["LIMIT", 1]]
Contact Load (30.9ms) SELECT "salesforce"."contact".* FROM "salesforce"."contact" WHERE "salesforce"."contact"."sfid" = $1 LIMIT $2 [["sfid", "003m000000txXhwAAE"], ["LIMIT", 1]]
=> #<Contact sfid: "003m000000txXhwAAE", id: "003m000000txXhwAAE", [...etc...] >
2.5.1 :004 > inventory.contacts.count
(30.7ms) SELECT COUNT(*) FROM "salesforce"."contact" INNER JOIN "salesforce"."content_access__c" ON "salesforce"."contact"."sfid" = "salesforce"."content_access__c"."contact__c" WHERE "salesforce"."content_access__c"."inventory__c" = $1 [["inventory__c", "a1mm0000001S9qzAAC"]]
=> 0
So: everything seems to be hooked up correctly, so why isn't the through version working? Any help would be appreciated.
Thanks! ❤️
Thanks to @Zabba's comment asking about SalesforceModel, I was able to track down the problem. Fundamentally, it was this:
class ContentAccess < ApplicationRecord
It should have been:
class ContentAccess < SalesforceModel # <-- it was using the wrong table, effectively.
This has been a harrowing week, but it's better now. 😀
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With