2011-12-21 31 views
0

我是新來的Mongo和Mongoid,並有一個相當複雜的數據模型,我試圖從根本上查詢多對多關係,並沒有得到任何數據即使我已經驗證了ID匹配。Mongoid多對多查詢返回非文檔

的數據模型如下(歡迎提出意見,如果更好的方式來做到這一點):

class User 
    has_one :item_list #favorite items 
    has_one :store_list #favorite stores 

class ItemList 
    belongs_to :user 
    has_and_belongs_to_many :items 

class StoreList 
    belongs_to :user 
    has_and_belongs_to_many :stores 

class Item 
    belongs_to :artist 
    has_and_belongs_to_many :stores 
    has_and_belongs_to_many :item_lists 

class Store 
    has_many :versions 
    has_and_belongs_to_many :store_lists 

class Version 
    belongs_to :item 
    belongs_to :store 

在暗示我在線閱讀,我試圖在特定的商店獲得用戶的收藏列表中的項目版本(雖然只留下特定商店的部分,因爲甚至不能跨版本的完整列表獲取),像這樣:

@favorite_item_ids = current_user.item_list.items.only(:_id).map(&:_id) 
@my_items_here = Version.all_in(item_id: @favorite_item_ids) 

我打印出編號,這是因爲這樣所以應該是至少1場,但@my_items_here有長度0

@favorite_item_ids
[BSON ::的ObjectId( '4ede1ec254663443fe000011'),...]

Version.all.only(:ITEM_ID).MAP(&:ITEM_ID)
[BSON ::的ObjectId( '4ede1ec254663443fe000011'),...]

感謝任何幫助!

版本: mongoid 2.3.4寶石 蒙戈2.0.1 的Rails 3.1

+0

對此有何更新?如果他回答你的問題,請接受答案。 – 2012-01-04 21:22:04

回答

1

試試這個:

@favorite_item_ids = current_user.item_list.items.distinct(:_id) 
@my_items_here = Version.where(:item_id.in => @favorite_item_ids) 
+0

工程就像魅力! – RSB 2015-08-21 11:35:20