2013-11-21 134 views
0

我使用mongoid_nested_set,我怎麼能簡化驗證碼:回到第二和第三個記錄

category.ancestors.where(depth: 3).first 

以前我用:

category.ancestors[3] 

但是,這似乎並沒有在mongoid 3.1工作。 5/rails 3.2.13/ruby​​ 1.9.3/acticeadmin-mongoid 0.3.0

+0

它應該工作。發生了什麼? –

+0

如果我運行'category.ancestors [3]' 我得到一個運行時錯誤,錯誤說只有'3' –

+0

'ancestors'不是一個Mongoid方法,請指定您正在使用的gem。 – Stefan

回答

0

最後罪魁禍首是acticeadmin_mongoid 0.3.0,我已經刪除它,一切工作正常。

1

您是否使用祖先寶石?

如果你這樣做,你可以使用自己的命名範圍 在這種情況下

category.ancestors.to_depth(3) # Return nodes up to depth 3 (1 and 2 also included) 
category.ancestors.at_depth(depth) # Return nodes with the exact depth of 3 

README看到。

+0

不幸的是我使用了mongoid_nested_set,但是如果我使用祖先,這將是完美的。謝謝 –

相關問題