Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-4769

embeds_many mishandles positional projections (foo.$)

      I have a `SingleSelectQuestion` model which `embeds_many :select_options`. In Mongoid 6.4.4, I can do this query to get me the first select option embedded within a `SingleSelectQuestion` that is active. Active is a Ruby method, defined as "status == :active".

      Here's the query:

       

      SingleSelectQuestion
        .where(
          id: question.id.to_s,
         "select_options._id" => question.select_options.first.id
        )
        .only("select_options.$")
        .first
        .select_options
        .detect(&:active?)
      

       

       There is only ever one "active" select option here. Due to how our database is constructed (the use of embeds many), I feel like this is the best way to do this query.

      The thing I want to talk about today is that `only("select_options.$")` syntax.

      When I run this code using the latest `mongodb/mongoid` master commit from today (currently b598df2dd78d), this query returns:

       

      >> SingleSelectQuestion.where(id: question.id.to_s, "select_options._id" => question.select_options.first.id).only("select_options.$").first.select_options.detect(&:active?) Traceback (most recent call last): 3: from (irb):8 2: from (irb):8:in `detect' 1: from app/models/select_option.rb:74:in `active?' ActiveModel::MissingAttributeError (Missing attribute: 'status'.) 

      I think the behaviour of `only` has regressed / changed here, as it appears the syntax of "select_options.$" is NOT including all the fields as it has done in the past.

      Has something changed here? Is this a bug? Or is this an incorrect use of mongoid?

       

       

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            me@ryanbigg.com Ryan Bigg
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: