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

Localized field predicate methods do not respect the current locale

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 5.1.3
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:
      Ruby on Rails 4.2.5, Mongoid 4.0.2, MongoDB 3.0.4

      When setting a localized Hash field to `nil` after it's been created, the predicate method corresponding to that field does not respect the locale.

      Consider the following Mongoid model called "Product":

      class Product
        include Mongoid::Document
      
        field :details, type: Hash, localize: true
      end
      

      It has a single, localized attribute called "details". Here's what happens when we try to create a Product using the locale "es":

      I18n.locale = :es
      product = Product.create details: { foo: 'bar' }
      product # => ... details: { "es" => { foo: "bar" } } ...
      product.details? # => true
      product.details # => { foo: 'bar' }
      

      Now watch what happens after we update the "details" attribute and run the same calls on the model:

      product.update details: nil
      product # => ... details: { "es" => nil } ...
      product.details? # => true
      product.details # => nil
      

      This inconsistency would then cause the following example implementation code to fail:

      if @product.details?
        @product.details.each { |detail| puts detail }
      end
      

      with a very nasty "undefined method each for nil:nilClass" error.

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            tubbo Tom Scott
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: