ruby-on-railsmongodbmongoidmongoid3mongoid4

Filter nested attributes with ruby mongoid gem


I have define the scope below (within my model) to help me filter out certain un-required nested data.

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

When I run this I get results are contains other events like "in_progress" that this scope should not contain.


Solution

  • I think your code should be rewritten into

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