【问题标题】:Tire : Eager loading associations from multiple models轮胎:来自多个模型的渴望加载关联
【发布时间】:2014-01-03 22:22:40
【问题描述】:

我在我的应用程序中使用 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

错误。我认为这是因为主题模型没有关联 cmets,我认为使用 Article 模型的帖子也会发生同样的事情。

有没有办法解决这个问题?我在想可能是这样的

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

请帮帮我。

【问题讨论】:

    标签: ruby-on-rails search include tire


    【解决方案1】:

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

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

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

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

    希望这会有所帮助。

    【讨论】:

    猜你喜欢
    • 2013-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-30
    • 2021-07-04
    • 2015-02-24
    • 1970-01-01
    相关资源
    最近更新 更多