unable to access parent when setting child attribute

XMLWordPrintableJSON

    • None
    • None
    • None
    • None
    • None
    • None

      When setting child attribute via nested_attribute, we cannot access its parent. I've created a demo app to demonstrate the issue.

      class Image
        include Mongoid::Document
        embedded_in :page
      
        def bam= value
          puts 'calling bam='
          byebug
          puts "page: #{page}" # This is nil. Incorrect.
        end
      end
      
      class Page
        include Mongoid::Document
        embeds_many :images, cascade_callbacks: true
        accepts_nested_attributes_for :images
      end
      
      attributes = {
        'images_attributes' => {
          '0' => {
            'bam' => 'bam'
          }
        }
      }
      page = Page.new
      page.attributes = attributes
      
      puts 'after setting attributes'
      puts page.images.first.page
      

              Assignee:
              Neil Shweky (Inactive)
              Reporter:
              khoa nguyen
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Created:
                Updated:
                Resolved: