gpt4 book ai didi

ruby-on-rails - 不能急切地加载多态关联

转载 作者:行者123 更新时间:2023-12-04 12:52:20 24 4
gpt4 key购买 nike

我收到错误
无法急切地加载多态关联:messageable_fromuser

online = Message.all.joins(:messageable_fromuser)

我试过
online = Message.all.includes(:messageable_fromuser)

但它不包括结果中的连接表。使用包含时,我在日志中看到两个查询。我不知道为什么人们推荐使用包含来急切加载。两个查询如何连接任何东西?

最佳答案

使用多态,您需要手动设置连接。

Message.joins("JOIN polymorphic_association.owner_id = messages.id AND polymorphic_association.owner_type = 'User'")

如果您想动态获取关系,您可以执行以下操作:
owner_type = 'User' # or whatever other models can have the association
Message.joins("JOIN polymorphic_association.owner_id = messages.id AND polymorphic_association.owner_type = ?", owner_type)

关于ruby-on-rails - 不能急切地加载多态关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26372613/

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