2015-07-28 39 views
0

你能幫我思考獅身人面像嗎?思維獅身人面像有很多通過導軌

關係:

大陸 的has_many:國家 的has_many:country_reports,通過:國家:CLASS_NAME => 「報告」 的has_many:出版者,通過:country_reports

預期輸出:

我想找到Continent.first.publishers

請告訴我如何寫在思維獅身人面像軌道

回答

1

正如我回答的思考獅身人面像谷歌組:

因爲你在出版商搜索,這是你需要修改,以得到這個工作,發佈指數。我假設發佈者belongs_to:country_report,國家報告belongs_to:國家和國家belongs_to:大陸。

如果你使用SQL支持指數(使用:用=>:active_record選項),那麼你會希望在你的發行指標如下:

has country_report.country.continent_id, :as => :continent_id 

如果你使用真正的 - 時間指數(:用=>:REAL_TIME),那麼它是相同的,但你必須指定類型,以及:

has country_report.country.continent_id, :as => :continent_id, :type => :integer 

但是,如果在從鏈協會是一個的has_many或has_and_belongs_to_many代替belongs_to的的出版商到大陸,那麼它有點複雜。此外,在這種情況下,發佈商可能有多個大陸,因此我們在此處理多個值。

對於SQL支持指數,一個微小的變化,改變的關聯鏈:

has country_reports.country.continent_id, :as => :continent_ids 

但隨着實時指數,最好是有一個返回所需的值在發佈模型的方法或值,然後使用該索引:

# in app/models/publisher.rb 
def continent_ids 
    country_reports.collect(&:country).collect(&:continent_id) 
end 

# in app/indices/publisher_index.rb 
has continent_ids, :type => :integer, :multi => true 

然後,一旦你重建索引,您可以搜索如下(與屬性是多如適用):

Publisher.search 「foo」, :with => {:continent_id => continent.id} 

可能工作,以及(關聯的多層次,雖然可以混淆的東西):

continent.publishers.search 「foo」 
相關問題