2013-07-16 58 views
0

的號碼,我有這樣的事情在我的模型:輪胎 - 按相關對象

class User < ActiveRecord::Model 

    has_many :followers, :through => :as_followed_followings, :class_name => "User", :foreign_key => "follower_id", :uniq => true 

    ... 

    def self.search(params) 
    tire.search(load: true, page: params[:page], per_page: params[:per]) do 
    end 
    end 

end 

我想返回追隨者的計數用戶已訂購用戶的數組。

那麼定義映射和索引以通過count屬性搜索嵌套對象的正確方法是什麼?

感謝您的閱讀。

回答

-1

與嘗試......

AsFollowedFollowing.count(:group => :follower_id, :order => "count_all DESC") 

這是有序散列如果你要循環這個散列包含user_id => count

,嘗試以下

# in controller 
@counts = AsFollowedFollowing.count(:group => :follower_id, :order => "count_all DESC") 
@users = User.where(:id => @counts.map{|k,_| k}) 

#in view 
<% @counts.each do |k, v| %> 
    <% user = @users.find{|u| u.id == k} %> 
    <div><%= user.name %> | <%= v %></div> 
<% end %> 
+0

是'count'從elasticsearch DSL? – nXqd

+0

對不起,其實我不知道elasticsearch。 count是ActiveRecord :: Model的方法。 –

+0

感謝您的回答,但它是關閉主題,因爲我的問題是關於elasticsearch和輪胎寶石:) – nXqd

0

我已經解決了這個問題在users表上創建另一個字段併爲該值創建索引,而不是在運行時執行嵌套搜索。

這可能不是最好的解決方案,但我有截止日期。任何更好的解決方案是歡迎:)