gpt4 book ai didi

ruby-on-rails - 轮胎 : Eager loading associations from multiple models

转载 作者:行者123 更新时间:2023-12-02 03:40:08 25 4
gpt4 key购买 nike

我在我的应用程序中使用 elasticsearch 进行查询。假设有 2 个模型

Class Topic
has_many :posts

Class Article
has_many :comments

我想对这两个模型进行组合搜索,我的查询如下所示:

Tire.search [Article, Topic], {:load => {:include => [:posts, :comments]}} do |search|
...
end

这是我遇到的问题。我明白了

Association named comments not found

错误。我认为这是因为主题模型没有关联评论,我认为文章模型的帖子也会发生同样的事情。

有什么办法可以解决这个问题吗?我在想也许像

:include => ['topic.posts', 'article.comments']

请帮帮我。

最佳答案

我刚刚根据在 this post 上找到的一些指导解决了一个类似的问题.这个答案说试试这个:

 Tire.search [Article, Topic], {:load => { Article =>{ :include => :comments}, 
Topic => { :include => :posts }
} } do |search|

在我的例子中,我需要一个来加载嵌套关联,其中我有一个两层深的层次结构,我希望 Tire 急切地加载它。所以我根据我的情况调整了该解决方案:

Tire.search 'articles', {:load => { :include => [:contributors, 
:books => [:authors]] } } do

希望这对您有所帮助。

关于ruby-on-rails - 轮胎 : Eager loading associations from multiple models,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20612422/

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