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

cannot access parent object in the after_find function

    • Minor Change

      I have two models:

      class Npdb
        include Mongoid::Document
        field :number, type: String
        field :number_type, type: String
        field :_id, type: String, default: ->{ number }
      
        embeds_many :npdb_moves, store_as: 'moves'
        accepts_nested_attributes_for :npdb_moves
      end
      

      and

      class NpdbMove
        include Mongoid::Document
        field :np_operator_code, type: String
        field :valid_from, type: Date
        field :net_prefix, type: String
        field :full, as: :country_meaning_code, type: String
        field :company, type: String
        field :_id, type: String, default: ->{ valid_from }
      
        embedded_in :npdb
        index({valid_from: -1})
      
        validates_presence_of   :valid_from
        validates_uniqueness_of :valid_from
      
        after_find :fix_empty_prefixes
      
        protected
        def fix_empty_prefixes
          return if net_prefix.present?
      
          p npdb #returns nil
          p npdb? #returns false
      end
      

      For me it seems logical, if I will have an access to the parent model in the fix_empty_prefixes method. Or should I use something else to get to parent object?

      P.S. The same problem with the callback after_initialize

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            kritik kritik
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: