Mongomapper Scope Concatenation strange behaviour

9 views
Skip to first unread message

Andrea Cirulli

unread,
Jun 13, 2017, 4:52:29 PM6/13/17
to MongoMapper

I'm experiencing strange behaviour with mongomapper scope concatenation.

Below an example.

I have two scopes:

 scope :active, where(
    :name =>
    {
      :$in =>
      Model2.active.distinct(:city)
    }
  )
  scope :by_htype_id, lambda{|htype_id|
      where(
        :name =>
        {
          :$in =>

            Model2.by_htype_id(htype_id).distinct(:city)

        }
      )
  }

If I run

Model1.by_htype_id("some_id") it works as expected

but if I concatenate the two scopes Model1.active.by_htype_id("some_id") I obtain all the result from the active scope while I would expect to obtain the subset of active scope that depends on by_htype_id

Jon Kern

unread,
Jun 14, 2017, 6:11:03 AM6/14/17
to mongo...@googlegroups.com
Forgetting about chaining scopes… can you write your query and get it to work without scopes?

Something looks strange below… You use “Model1” and “Model2” … 

1) Are these scopes in Model1?
2) both scopes are doing a “filter” of Model1 name being in a list of names generated by a Model2 identical scopes?

--
You received this message because you are subscribed to the Google
Groups "MongoMapper" group.
For more options, visit this group at
http://groups.google.com/group/mongomapper?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups "MongoMapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongomapper...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jamie Orchard-Hays

unread,
Jun 14, 2017, 11:38:24 AM6/14/17
to mongo...@googlegroups.com
Does MM say chaining scopes is available? I would think that would be very complicated to automagically work out.

Jamie

On Jun 13, 2017, at 4:52 PM, Andrea Cirulli <acir...@gmail.com> wrote:

Andrea Cirulli

unread,
Jun 15, 2017, 1:50:46 PM6/15/17
to MongoMapper
Sure I can write a query by myself but using scopes chaining is very useful for keeping the code clean and reusable.

For what concerning my examples I also tried to make a unique query that simulates the chaining and works as expected.

Below the query:

Model1.where(:$and =>[{:name => {:$in =>Model2.active.distinct(:city)}},{:name => {:$in =>Model2.by_htype_id(htype_id).distinct(:city)}}])

Andrea Cirulli

unread,
Jun 15, 2017, 1:52:41 PM6/15/17
to MongoMapper
If you read the documentation at this link: http://mongomapper.com/documentation/plugins/scopes.html you can read this example:
# chaining class methods on scopes pp User.by_ages(20, 40).by_tag('ruby').all

So I do not expect the chaining to automagically work out.......

Andrea Cirulli

unread,
Jun 15, 2017, 1:53:45 PM6/15/17
to MongoMapper
In fact most of the time chaining work as expected, but in some cases like the one I proposed it does not. It seems that if the scope i working on the same field it does not work.

Jamie Orchard-Hays

unread,
Jun 15, 2017, 3:24:36 PM6/15/17
to mongo...@googlegroups.com
Chaining simple queries makes sense. Once you chain non-simple queries, things explode in complexity. 

I would expect chaining clauses that operate on the same field to fail. Do you want an OR or an AND statement? Trying out, last one in the chain wins. I did this in the mongo shell. 

For example:

db.substances.find({_id: "422-49-1", _id: "505-60-2"}, {_id:1}).limit(10)
{ "_id" : "505-60-2" }

You can only find one substance for an _id value. 

It’s not exactly your scenario, but perhaps that helps illuminate the problem. 

Jamie
Reply all
Reply to author
Forward
0 new messages