mongoid/mongoid — Issue #1762: any_of does not localize fields
Author: teunvanvegchel
Created: 2012-02-26 15:39:57+00:00
State: open

any_of doesn't suffix the field key with the locale if more than one condition is provided.

# Working fine:
Post.any_of title: 'Hello World' # => { :title.en => 'Hello World' }

# Working not so fine:
Post.any_of title: 'Hello World', description: 'Hello World' # => { :title => 'Hello World', :description => 'Hello World' }

I looked around a little, and it seems to be caused by the []= method in the Selection module because it doesn't recognize these kinds of nested keys. I changed ```handle_and_or_value(values)``` to localize keys and now things work great.

def handle_and_or_value(values)
[].tap do |result|
result.push(*values.map do |value|
Hash[value.map{ |key, value| key = "#{key}.#{::I18n.locale}" if klass.fields[key.to_s].try(:localized?); [key, try_to_typecast(key, value)] }]
end)
end
end