2011-12-21 67 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