has_many:通过不加载记录

时间:2018-08-28 20:20:55

标签: ruby-on-rails

我有一个Rails 5.2.1应用程序,关系的每个步骤都可以使用,但是has_many :through版本却不行。设置有些奇怪,但是我觉得我已经正确设置了所有内容,所以有点困惑。

给出以下代码:

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

has_many的每一步:通过工作:

# 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: "john.doe@realisp.com", 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 

虽然可以在Postgres中运行生成的查询,但可以:

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)

运行Contact.first.concepts.to_sql会产生:

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'

通过psql运行该查询可以正常工作,并从清单__c表中返回正确的记录。

反向也有相同的问题:

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 

所以:似乎所有东西都正确连接了,那么贯通版本为什么不起作用?任何帮助将不胜感激。

谢谢! ❤️

1 个答案:

答案 0 :(得分:2)

由于@Zabba的评论询问了SalesforceModel,因此我能够找到问题所在。根本上是这样的:

class ContentAccess < ApplicationRecord

应该是:

class ContentAccess < SalesforceModel # <-- it was using the wrong table, effectively.

这是一个令人痛苦的一周,但现在好了。