AdamT
AdamT

Reputation: 6485

Not Equal (ne) and OR not returning correct results in Mongomapper scopes

I can't chain these two scopes together in Mongomapper using an OR:

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

It should return model objects where the comment is not nil, OR the post is not blank.

This doesn't work:

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

Any ideas?

Upvotes: 1

Views: 423

Answers (1)

mu is too short
mu is too short

Reputation: 434985

Chaining scopes is an and operation so M.comment_is_nil.post_not_blank won't work as you know. MongoDB's or syntax looks like this:

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

So you need to give it an array of individual conditions by manually expanding the scopes.

Upvotes: 2

Related Questions