【问题标题】:Mongoid Many-to-Many Query Returns Not DocsMongoid 多对多查询返回不是文档
【发布时间】:2011-12-21 02:15:31
【问题描述】:

我是 Mongo 和 Mongoid 的新手,并且有一个相当复杂的数据模型,我试图从本质上查询多对多关系并且没有获取任何数据,即使我已经验证了 id 似乎匹配。

数据模型如下(如果有更好的方法,欢迎提出建议):

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)

我正在打印 id,因此应该至少有 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 导轨 3.1

【问题讨论】:

  • 这有什么更新吗?如果他回答了您的问题,请接受答案。

标签: mongodb mongoid


【解决方案1】:

试试这个:

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

【讨论】:

    猜你喜欢
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    相关资源
    最近更新 更多