Incongruency with #pluck with localized fields on loaded relations vs. scopes

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Duplicate
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • 🔵 Done
    • Ruby Drivers
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      There is currently an issue where #pluck with localized fields behaves differently on relation scopes which are loaded in memory vs. queried from the database.

      The reason is because loaded scopes (e.g. a loaded has_many relation) are instances of Mongoid::Association::Referenced::*::Enumerable, which delegate to Enumerable#pluck.

      Conversely, query scopes are instances of Mongoid::Criteria, which use Mongoid::Contextual::Mongo#pluck.

      The fix should be to ensure that the Mongoid::Criteria behavior prevails in all cases.

      The below example illustrates the surprising behavior:

       

      class Person
        include Mongoid::Document
      
        has_many :dogs
      end
      
      class Dog
        include Mongoid::Document
      
        field :name, localize: true
        field :color
        belongs_to :person
      end
      
      # wrong!
      Person.dogs.class #=> Mongoid::Association::Referenced::HasMany::Enumerable
      Person.dogs.pluck(:name) #=> [{"en" => "Fido", "fr" => "Fideaux"}]
      Person.dogs.pluck(:name_translations) #=> [nil]
      
      # correct/expected
      Person.dogs.where(color: :red) #=> Mongoid::Criteria
      Person.dogs.where(color: :red).pluck(:name) #=> ["Fido"]
      Person.dogs.where(color: :red).pluck(:name_translations) #=> [{"en" => "Fido", "fr" => "Fideaux"}]

       

       

            Assignee:
            Jamis Buck
            Reporter:
            Johnny Shields
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: