# Using a nested object as a model property

**URL:** <https://discuss.emberjs.com/t/using-a-nested-object-as-a-model-property/6745>\
**Category:** Ember Data\
**Created:** [November 19, 2014, 6:07pm UTC](https://discuss.emberjs.com/t/using-a-nested-object-as-a-model-property/6745 "2014-11-19T18:07:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mheisig](https://avatars.discourse-cdn.com/v4/letter/m/35a633/32.png) [@mheisig](https://discuss.emberjs.com/u/mheisig)\
**Post date:** [November 19, 2014, 6:07pm UTC](https://discuss.emberjs.com/t/using-a-nested-object-as-a-model-property/6745/1 "2014-11-19T18:07:09Z")

</div>

I’m new to Ember and I’m trying to figure out if I’m missing something with using nested objects as model properties in Ember Data.

Given a JSON format like this:

```
{
    "name": "Some Name",
    "address": {
        "street": "123 Main Street",
        "city": "New York",
        "state": "NY"
    }
}

```

I can have a model like this:

```
name: DS.attr('string'),
address: DS.attr()

```

I can access the properties of the address object:

```
Person.get('address.street')

```

It seems to work fine in templates and I can used computed properties that observe the “address” property.

Am I missing something? Is this a bad pattern to follow?

---

<div class="post-metadata">

**Author:** ![kgish](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/kgish/32/13333_2.png) [@kgish](https://discuss.emberjs.com/u/kgish)\
**Post date:** [November 20, 2014, 2:07pm UTC](https://discuss.emberjs.com/t/using-a-nested-object-as-a-model-property/6745/2 "2014-11-20T14:07:16Z")

</div>

You might want to try the following:

```
App.User = DS.Model.extend({
    name: DS.attr('string'),
    address: DS.belongsTo('address')
});

App.Address = DS.Model.extend({
    street: DS.attr('string'),
    city: DS.attr('string'),
    state: DS.attr('string'),
    person: DS.belongsTo('person')
});

```

Otherwise you have to “guess” the underlying data elements of the address object whereas by using the above you have explicitly defined it.

I believe that this is best practice.

---

<div class="post-metadata">

**Author:** ![ldmello1](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ldmello1/32/11443_2.png) [@ldmello1](https://discuss.emberjs.com/u/ldmello1)\
**Post date:** [October 6, 2015, 2:05pm UTC](https://discuss.emberjs.com/t/using-a-nested-object-as-a-model-property/6745/3 "2015-10-06T14:05:57Z")

</div>

Lets say we wanted to serialize a response returned by an adapter to the user model.

Will we have to create explicit non unique _ **id** _ field for each _ **Address** _ model record that we put into a _ **User** _ model record?

Or, since it is a belongsTo relationship, the _ **id** _ for the _ **User** _ model record should suffice?

---

<div class="post-metadata">

**Author:** ![jasonmit](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jasonmit/32/182_2.png) [@jasonmit](https://discuss.emberjs.com/u/jasonmit)\
**Post date:** [October 7, 2015, 6:32am UTC](https://discuss.emberjs.com/t/using-a-nested-object-as-a-model-property/6745/4 "2015-10-07T06:32:18Z")

</div>

ember-model-fragments solves this story: [GitHub - adopted-ember-addons/ember-data-model-fragments: Ember Data addon to support nested JSON documents](https://github.com/lytics/ember-data.model-fragments)

---

<div class="post-metadata">

**Author:** ![ldmello1](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ldmello1/32/11443_2.png) [@ldmello1](https://discuss.emberjs.com/u/ldmello1)\
**Post date:** [October 7, 2015, 9:46pm UTC](https://discuss.emberjs.com/t/using-a-nested-object-as-a-model-property/6745/5 "2015-10-07T21:46:25Z")

</div>

Oh, that looks nifty. What I implemented is by use of transforms. I create an ember object and set it as a field of a parent model. But now what I see in the inspector is, if I change the nested ember object, the outer parent model object is not set to dirty ☹
