【问题标题】:mongoid .where queries not working as expectedmongoid .where 查询未按预期工作
【发布时间】:2016-10-11 14:43:13
【问题描述】:

我有一个属于另一个的模型:

class Permission
  include Mongoid::Document
  belongs_to :action
  field :action_id, type: String

还有一个对应的has_many模型:

class Action
  include Mongoid::Document
  has_many :permissions

我看到 2 个基本的 ActiveRecord 查询不起作用:

首先,发送对象而不是对象id是行不通的:

action = Action.first
permissions = Permission.where(action: action)
permissions.count # this is always 0

但是,如果我使用 id,它可以正常工作:

Permission.where(action_id: action.id) # this works fine

其次,发送一个 id 数组是行不通的:

Permission.where(action_id: [action.id]) # always empty

mongoid 不支持基本的 ActiveRecord 查询吗?如果不是,对于第一个问题,我可以通过始终使用对象 id 来解决它,但对于第二个问题,我如何通过目标数组进行搜索?

感谢您的帮助, 凯文

【问题讨论】:

标签: ruby-on-rails mongoid


【解决方案1】:

查看 Ruby on Rails 保留字。有可能是导致 .where 中断的原因。

http://www.rubymagic.org/posts/ruby-and-rails-reserved-words

【讨论】:

    猜你喜欢
    • 2021-02-06
    • 2012-11-13
    • 2014-08-03
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多