2013-12-16 50 views
3

我正在使用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 

錯誤。我認爲這是因爲Topic模型沒有關聯評論,我認爲文章模型的帖子也會發生同樣的情況。

有沒有辦法解決這個問題?我在想也許像

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

請幫我。

回答

1

我剛剛解決了一個類似的問題,根據我在this post上發現的一些指導。這個問題的答案說要試試這個:

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

就我而言,我需要加載嵌套的關聯,在那裏我有兩個深層次,我想輪胎熱切負載。所以我適應了我的情況下的解決方案:

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

希望這會有所幫助。

+1

我用過。對不起,沒有發佈答案。和FYI,這就是我所說的:https://github.com/karmi/retire/issues/762 –