【问题标题】:Filter nested attributes with ruby mongoid gem使用 ruby​​ mongoid gem 过滤嵌套属性
【发布时间】:2016-03-22 12:36:29
【问题描述】:

我已经定义了下面的范围(在我的模型中),以帮助我过滤掉某些不需要的嵌套数据。

scope :active_inactive, -> { self.in({
      state: ["current"],
      "events.type" => [
        :active,
        :inactive,
      ]
    }).desc(:created_at) 
  }

当我运行它时,我得到的结果包含其他事件,例如此范围不应包含的“in_progress”。

【问题讨论】:

    标签: ruby-on-rails mongodb mongoid mongoid3 mongoid4


    【解决方案1】:

    我觉得你的代码应该改写成

    scope :active_inactive, -> {
      self.where(:state.in => ["current"], :"events.type".in =>["active","inactive"]}).desc(:created_at) 
    }
    

    【讨论】:

    • 快到了@vitali
    猜你喜欢
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    • 2012-05-09
    • 2016-02-27
    相关资源
    最近更新 更多