# Mongoid - Inheritance: change embedded document's type via nested attributes

DevFeed: [Mongoid - Inheritance: change embedded document's type via nested attributes](<https://devfeed.tech/articles/mongoid-inheritance-change-embedded-document-s-type-via-nested-attributes-37450.md>)

Original publisher: [Read original article](<https://iridakos.com/programming/2019/11/22/mongoid-nested-attributes-embedded-type>)

Author: Lazarus Lazaridis

Published: 2019-11-22T07:00:00Z

Content type: tutorial

Language: en

Sources: [Lazarus Lazaridis](<https://devfeed.tech/sources/lazarus-lazaridis.md>)

Topics: [file](<https://devfeed.tech/topics/file.md>), [Rails](<https://devfeed.tech/topics/rails.md>), [Polymorphism](<https://devfeed.tech/topics/polymorphism.md>), [Forms](<https://devfeed.tech/topics/forms.md>)

Tags: [class](<https://devfeed.tech/tags/class.md>), [code](<https://devfeed.tech/tags/code.md>), [create](<https://devfeed.tech/tags/create.md>), [how-to](<https://devfeed.tech/tags/how-to.md>), [inheritance](<https://devfeed.tech/tags/inheritance.md>), [mongodb](<https://devfeed.tech/tags/mongodb.md>), [mongoid](<https://devfeed.tech/tags/mongoid.md>), [opensource](<https://devfeed.tech/tags/opensource.md>), [programming](<https://devfeed.tech/tags/programming.md>), [rails](<https://devfeed.tech/tags/rails.md>), [ruby](<https://devfeed.tech/tags/ruby.md>), [tutorial](<https://devfeed.tech/tags/tutorial.md>)

## AI overview

This tutorial examines problems that arise when changing the type of an embedded Mongoid document through nested attributes. Using a Rails console example with Book and BoardGame subclasses of Item, it demonstrates failed attribute assignments, leftover attributes, and incorrect validators caused by the in-memory object retaining its previous type.

## Source excerpt

I recently faced some issues when trying to update a nested document's type via nested attributes using the mongoid gem. Bare with me if this sentence doesn't make any sense yet. Use case Suppose we have a document named Person that embeds a document that can be any of the types Book or BoardGame which inherit from the class Item. Item class Item include Mongoid::Document embedded_in :person, inverse_of: :item field :name end Book class Book < Item field :authors, type: Array end BoardGame class BoardGame < Item field :players, type: Integer end Person class Person include Mongoid::Document embeds_one :item, inverse_of: :person accepts_nested_attributes_for :item, allow_destroy: true end The problems To simplify the post I won't go with the scenario of building the forms and the controller actions to demonstrate the problems but instead I will simulate the behavior in the rails console. Let's create a Person with a Book as an Item. >> person = Person.create!({ item_attributes: { _type: "Book", name: "Jitterbug Perfume", authors: ["Tom Robbins"] } }) => #<Person _id: 5dd7b01ec2889856ab8a619f, > >> person.item => #<Book _id: 5dd7ae76c2889856ab8a619e, name: "Jitterbug Perfume", _type: "Book", authors: ["Tom Robbins"]> Failing attribute assignments Let's try to change the person's item from Book to BoardGame with 5 players. >> person.update_attributes({ item_attributes: { _type: "BoardGame", name: "Ticket to Ride", players: 5 } }) Traceback (most recent call last): 1: from (irb):9 Mongoid::Errors::UnknownAttribute () message: Attempted to set a value for 'players' which is not allowed on the model Book. summary: Without including Mongoid::Attributes::Dynamic in your model and the attribute does not already exist in the attributes hash, attempting to call Book#players= for it is not allowed. This is also triggered by passing the attribute to any method that accepts an attributes hash, and is raised instead of getting a NoMethodError. resolution: You can include Mongoid::