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

After initialize callback is called before embedded association is initialized

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.1.3
    • Affects Version/s: None
    • Component/s: None

      Example:

      class User
      include Mongoid::Document
      embeds_one :address
      end

      class Address
      include Mongoid::Document
      embedded_in :user

      after_initialize :say_owner
      def say_owner
      puts "I'm embedded in #

      {user.inspect}

      "
      end
      end

      >> user = User.new
      => #<User _id: 4d9d532a8eed0498b7000003, _type: nil, _id: BSON::ObjectId('4d9d532a8eed0498b7000003')>
      >> user.build_address
      I'm embedded in nil
      => #<Address _id: 4d9d532d8eed0498b7000004, _type: nil, _id: BSON::ObjectId('4d9d532d8eed0498b7000004')>
      >> user.address.say_owner
      I'm embedded in #<User _id: 4d9d532a8eed0498b7000003, _type: nil, _id: BSON::ObjectId('4d9d532a8eed0498b7000003')>
      => nil

            Assignee:
            Unassigned Unassigned
            Reporter:
            semaperepelitsa semaperepelitsa
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: