我正在使用 Mongoid 并嘗試做一些類似于 ActiveRecord 的collection?方法。
我有一個從類中獲取關系的方法,例如:
def relations
object.class.relations
end
這使得關系物件的哈希。
然后在另一種方法中我這樣做:
if relations[key].collection?
do stuff...
else
do something else...
end
collection?在這種情況下,它不像在 ActiveRecord 中那樣是一種方法。所以我得到
undefined method `collection?' for #<Mongoid::Association::Referenced::BelongsTo:0x00007feed3b61240>
做這種條件的 Mongoid 方法是什么?
uj5u.com熱心網友回復:
我假設你在談論這個方法,這似乎只是在 has_many 和 HABTM 關聯上回傳 true ......從 8.0 開始,我們有many?方法來檢查它是 HasMany 還是 EmbedsMany,但我沒有不要認為這正是你所需要的。我現在可以建議一種方法,我會打開一張票,看看我們是否可以添加這個:
def collection?(association)
[
Mongoid::Association::Referenced::HasMany,
Mongoid::Association::Referenced::HasAndBelongsToMany
].any? { |r| association.is_a?(r) }
end
編輯:您可以在此處跟蹤此問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/526703.html
標籤:轨道上的红宝石关系蒙古族
