lib/mongoid/fields.rb
# Get the name of the provided field as it is stored in the database.
# Used in determining if the field is aliased or not. Recursively
# finds aliases for embedded documents and fields, delimited with
# period "." character.
#
# This method will not expand the alias of a belongs_to association that
# is not the last item. For example, if we had a School that has_many
# Students, and the field name passed was (from the Student's perspective):
#
#   school._id
#
# The alias for a belongs_to association is that association's _id field.
# Therefore, expanding out this association would yield:
#
#   school_id._id
#
# This is not the correct field name, because the intention here was not
# to get a property of the _id field. The intention was to get a property
# of the referenced document. Therefore, if a part of the name passed is
# a belongs_to association that is not the last part of the name, we
# won't expand its alias, and return:
#
#   school._id
#
# If the belongs_to association is the last part of the name, we will
# pass back the _id field.
#
# @param [ String, Symbol ] name The name to get.
# @param [ Hash ] relations The associations.
# @param [ Hash ] alaiased_fields The aliased fields.
# @param [ Hash ] alaiased_associations The aliased associations.
#
# @return [ String ] The name of the field as stored in the database.
#
# @api private
Get the name of the provided field as it is stored in the database.
Used in determining if the field is aliased or not. Recursively
finds aliases for embedded documents and fields, delimited with
period "." character.

This method will not expand the alias of a belongs_to association that
is not the last item. For example, if we had a School that has_many
Students, and the field name passed was (from the Student's perspective).

school._id

The alias for a belongs_to association is that association's _id field.
Therefore, expanding out this association would yield.



This is not the correct field name, because the intention here was not
to get a property of the _id field. The intention was to get a property
of the referenced document. Therefore, if a part of the name passed is
a belongs_to association that is not the last part of the name, we
won't expand its alias, and return.

school._id

If the belongs_to association is the last part of the name, we will
pass back the _id field.
Get the name of the provided field as it is stored in the database . Used in determining if the field is aliased or not . Recursively finds aliases for embedded documents and fields delimited with period " . " character . This method will not expand the alias of a belongs_to association that is not the last item . For example if we had a School that has_many Students and the field name passed was ( from the Student ' s perspective ) . school . _id The alias for a belongs_to association is that association ' s _id field . Therefore expanding out this association would yield . This is not the correct field name because the intention here was not to get a property of the _id field . The intention was to get a property of the referenced document . Therefore if a part of the name passed is a belongs_to association that is not the last part of the name we won ' t expand its alias and return . school . _id If the belongs_to association is the last part of the name we will pass back the _id field .
def database_field_name(name, relations, aliased_fields, aliased_associations)
        if Mongoid.broken_alias_handling
          return nil unless name
          normalized = name.to_s
          aliased_fields[normalized] || normalized
        else
          return nil unless name.present?
          key = name.to_s
          segment, remaining = key.split('.', 2)
          if !remaining || !relations.key?(segment) || !relations[segment].is_a?(Association::Referenced::BelongsTo)
            segment = aliased_fields[segment]&.dup || segment
          end
          return segment unless remaining
          relation = relations[aliased_associations[segment] || segment]
          if relation
            k = relation.klass
            "#{segment}.#{database_field_name(remaining, k.relations, k.aliased_fields, k.aliased_associations)}"
          else
            "#{segment}.#{remaining}"
          end
        end
      end
def database_field_name ( name , relations , aliased_fields , aliased_associations ) if Mongoid . broken_alias_handling return nil unless name normalized = name . to_s aliased_fields [ normalized ] || normalized else return nil unless name . present? key = name . to_s segment , remaining = key . split ( '.' , 2 ) if ! remaining || ! relations . key? ( segment ) || ! relations [ segment ] . is_a? ( Association :: Referenced :: BelongsTo ) segment = aliased_fields [ segment ] &. dup || segment end return segment unless remaining relation = relations [ aliased_associations [ segment ] || segment ] if relation k = relation . klass "#{segment}.#{database_field_name(remaining, k.relations, k.aliased_fields, k.aliased_associations)}" else "#{segment}.#{remaining}" end end end
Get the name of the provided field as it is stored in the database.
Get the name of the provided field as it is stored in the database .
# Don't get the alias for the field when a belongs_to association # is not the last item. Therefore, get the alias when one of the # following is true: # 1. This is the last item, i.e. there is no remaining. # 2. It is not an association. # 3. It is not a belongs association
The name of the field as stored in the database.
The name of the field as stored in the database .
 String 
name
<not_specific>
relations
<not_specific>
aliased_fields
<not_specific>
aliased_associations
<not_specific>
<not_specific>
alaiased_associations The aliased associations.
alaiased_associations The aliased associations .
api
None