-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
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.