2015-04-23 71 views
1

如何在訂單調用的上下文中獲取特定記錄的位置?從訂單獲取記錄位置?

例如:

username = User.find(1) 

User.all.order("created_at DESC").position_of(username) 

是否有這樣的事?

+0

難道我的回答幫助? –

+0

'each_with_index'可能有助於http://stackoverflow.com/questions/13421305/rails-display-chronological-order-of-number-of-records – bodyfarmer

回答

-1

Array#index方法:

user = User.find(1) 
index = User.order('created_at DESC').index(user) 
+0

問題是User.order(created_at :: desc)不是陣列。所以你會得到一個「未定義的方法'索引'# Asarluhi