mongodb/mongoid · error · Mongoid::Errors::ReadonlyAssociation

Attempted to modify the read-only association '%{name}' on '

Error message

Attempted to modify the read-only association '%{name}' on '%{klass}'.

What it means

Error "Attempted to modify the read-only association '%{name}' on '%{klass}'." thrown in mongodb/mongoid.

Source

Thrown at lib/mongoid/association/referenced/has_one_through.rb:157

        end

        def define_through_getter!
          assoc = self
          assoc_name = name
          @owner_class.re_define_method(assoc_name) do |reload = false|
            if reload || !instance_variable_defined?("@_#{assoc_name}")
              doc = assoc.criteria(self)
              proxy = doc ? HasOneThrough::Proxy.new(self, doc, assoc) : nil
              set_relation(assoc_name, proxy)
            end
            instance_variable_get("@_#{assoc_name}")
          end
        end

        def define_readonly_setter!
          assoc = self
          @owner_class.re_define_method("#{name}=") do |_object|
            raise Mongoid::Errors::ReadonlyAssociation.new(self.class, assoc)
          end
        end

        def default_primary_key
          PRIMARY_KEY_DEFAULT
        end
      end
    end
  end
end

View on GitHub (pinned to 0da0e23d71)

When it happens

Trigger: Thrown at lib/mongoid/association/referenced/has_one_through.rb:157 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of mongodb/mongoid@0da0e23d71 (2026-08-23). Data as JSON: /api/errors/4a8600bb1925b05f. Report an issue: GitHub.