【问题标题】:Not Equal (ne) and OR not returning correct results in Mongomapper scopes不等于 (ne) 和 OR 未在 Mongoapper 范围内返回正确结果
【发布时间】:2012-05-08 14:42:39
【问题描述】:

我无法在 Mongoapper 中使用 OR 将这两个范围链接在一起:

scope :comment_is_nil, where(:comment => nil)
scope :post_not_blank, where(:post.ne => "")

它应该返回评论不为零的模型对象,或者帖子不为空。

这不起作用:

Model.where("$or" => [{:comment_is_nil, :post_not_blank])

有什么想法吗?

【问题讨论】:

    标签: ruby mongodb model scope mongomapper


    【解决方案1】:

    链式作用域是一个 and 操作,因此 M.comment_is_nil.post_not_blank 不会像您所知的那样工作。 MongoDB's or syntax 看起来像这样:

    Model.where(
        :$or => [
            { :comment => nil },
            { :post.ne => ''  }
        ]
    )
    

    因此,您需要通过手动扩展范围来为其提供一系列单独的条件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-22
      • 2022-12-11
      • 1970-01-01
      • 2016-02-15
      • 1970-01-01
      • 2011-01-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多