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

Error when using store_as after upgrade to Ruby 3.2.2 & Rails 7.0

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 9.0.0, 8.0.6, 8.1.2
    • Affects Version/s: None
    • Component/s: None
    • None

      After upgrading our app from Ruby 3.1.2 to 3.2.2, Rails 6.1.4 to 7.0 and mongoid 7.5 to 8.0.3 when using embeds_many and embeds_one with the store_as attribute the app is failing with this message when trying to assign attributes undefined method `nested_builder' for nil:NilClass. 

      After tracing this error through to the mongoid gem, I could see that it came from this line, and seems to happen because the key for embedded associations with a store_as defined is different in the pending_relations hash to what it is in the relations hash. I can fix this issue with a work-around, changing the code in the `pending_attribute?` method to:

      def pending_attribute?(key, value)
        name = key.to_s
      
        aliased = if aliased_associations.key?(name)
                    aliased_associations[name]
                  else
                    name
                  end
      
        if relations.has_key?(aliased)
          pending_relations[aliased.to_s] = value
          return true
        end
        if nested_attributes.has_key?(aliased)
          pending_nested[name] = value
          return true
        end
        return false
      end
      

      Fixes my specific problem (storing the value in the ray with the key of `aliased.to_s` rather than the key of `name` that it was before.

      But I don't think this gets to the root cause as this code was working fine before, and did not change when upgrading from mongoid 7.5 to 8.03

       
       

            Assignee:
            dmitry.rybakov@mongodb.com Dmitry Rybakov
            Reporter:
            jerome.pratt@defra.gov.uk Jerome Pratt
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: