gpt4 book ai didi

ruby-on-rails - has_many :through not loading records

转载 作者:行者123 更新时间:2023-12-04 06:19:33 25 4
gpt4 key购买 nike

我有一个 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 :through 的每一步都有效:
# 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 运行该查询工作正常,从inventory__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

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

谢谢! ❤️

最佳答案

感谢@Zabba 关于 SalesforceModel 的评论,我能够找到问题所在。基本上是这样的:

class ContentAccess < ApplicationRecord

本来应该是:
class ContentAccess < SalesforceModel # <-- it was using the wrong table, effectively.

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

关于ruby-on-rails - has_many :through not loading records,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52065665/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com