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

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Ruby Drivers
    • 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:
            2 Start watching this issue

              Created:
              Updated: