# How is ember-data's support for loading embedded associations?

**URL:** https://discuss.emberjs.com/t/how-is-ember-datas-support-for-loading-embedded-associations/4382
**Category:** Ember Data
**Created:** [March 4, 2014, 9:35pm UTC](https://discuss.emberjs.com/t/how-is-ember-datas-support-for-loading-embedded-associations/4382 "2014-03-04T21:35:48Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![bemathis](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bemathis/32/5431_2.png) [@bemathis](https://discuss.emberjs.com/u/bemathis)
#### Post date: [March 4, 2014, 9:35pm UTC](https://discuss.emberjs.com/t/how-is-ember-datas-support-for-loading-embedded-associations/4382/1 "2014-03-04T21:35:48Z")

</div>

Using Ember Data : 1.0.0-beta.4

I’m working on my first ember app using a rails backend and activerecord serializers to generate JSON data for ember to use. My question is, how is ember’s support for embedded has\_many relationships?

For example, I would like to write a serializer like this

```ruby
class RegistrySerializer < ActiveModel::Serializer
  attributes :id, :name
  has_many :categories
end

```

Any my Ember models like this

```nohighlight
App.Registry = DS.Model.extend
  name: DS.attr('string')
  categories: DS.hasMany('Category', { embedded: 'always' })

App.Category = DS.Model.extend
  name: DS.attr('string')
  registry: DS.belongsTo('Registry')

```

---

<div class="post-metadata">

### Author: ![pixelhandler](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/pixelhandler/32/18649_2.png) [@pixelhandler](https://discuss.emberjs.com/u/pixelhandler)
#### Post date: [March 5, 2014, 9:00am UTC](https://discuss.emberjs.com/t/how-is-ember-datas-support-for-loading-embedded-associations/4382/2 "2014-03-05T09:00:48Z")

</div>

@bemathis the DS.EmbeddedRecordsMixin has good support for embedded ‘hasMany’ relationships, see the source code for more info: [https://github.com/emberjs/data/blob/v1.0.0-beta.6/packages/activemodel-adapter/lib/system/embedded\_records\_mixin.js](https://github.com/emberjs/data/blob/v1.0.0-beta.6/packages/activemodel-adapter/lib/system/embedded_records_mixin.js)

---

<div class="post-metadata">

### Author: ![mfeckie](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/mfeckie/32/15198_2.png) [@mfeckie](https://discuss.emberjs.com/u/mfeckie)
#### Post date: [March 5, 2014, 2:43pm UTC](https://discuss.emberjs.com/t/how-is-ember-datas-support-for-loading-embedded-associations/4382/3 "2014-03-05T14:43:46Z")

</div>

Your serializer can be instructed to include the items you want when the request is made.

```ruby
class RegistrySerializer < ActiveModel::Serializer
  attributes :id, :name
  has_many :categories
  embed :ids, include: true
end

```

There are some issues with the naming in your Ember models, for example the capitalization isn’t necessary. I’m also not sure that the `{ embedded: 'always' }` is necessary.

```auto
App.Registry = DS.Model.extend
  name: DS.attr('string')
  categories: DS.hasMany('category')

App.Category = DS.Model.extend
  name: DS.attr('string')
  registry: DS.belongsTo('registry')

```

How are you setting up your store? For example are you using the ActiveModelAdapter? The sample below will tell Ember how to interact with data from ActiveModelSerializers. By reopening the RESTAdapter and suppling the namespace this will be prepended to the request URL. In the example below getting your Registry index would hit api/v1/registrys

```javascript
App.Store = DS.Store.extend({
    adapter: '-active-model'
});

App.RESTAdapter.reopen(
    {namespace: "api/v1"}
);

```

Hope this helps.

---

<div class="post-metadata">

### Author: ![bemathis](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/bemathis/32/5431_2.png) [@bemathis](https://discuss.emberjs.com/u/bemathis)
#### Post date: [March 5, 2014, 3:00pm UTC](https://discuss.emberjs.com/t/how-is-ember-datas-support-for-loading-embedded-associations/4382/4 "2014-03-05T15:00:42Z")

</div>

I’m using the ActiveModelAdaptor in my store.coffee file

```nohighlight
App.ApplicationAdapter = DS.ActiveModelAdapter.extend
  namespace: 'api/v1'

```

I’ve managed to get Ember reading associations properly by doing what you suggested and telling my Serializer to embed the ids

```ruby
class RegistrySerializer < ActiveModel::Serializer
  embed :ids, include: true

  attributes :id, :name

  has_many :categories
end

```

I do find what you said about the store configs interesting thought. Would something like that be needed w/my current store setup? Is you config generally preferable over using `ActiveModelAdapter` which I am to useing the `1.0.0.beta7` of.

---

<div class="post-metadata">

### Author: ![mfeckie](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/mfeckie/32/15198_2.png) [@mfeckie](https://discuss.emberjs.com/u/mfeckie)
#### Post date: [March 5, 2014, 3:25pm UTC](https://discuss.emberjs.com/t/how-is-ember-datas-support-for-loading-embedded-associations/4382/5 "2014-03-05T15:25:24Z")

</div>

I think what you’re doing is fine. I can’t even remember where I picked up that configuration style! I haven’t found it to be a problem, so just kept using it. Having said that the syntax you’ve used is perfectly valid and more succinct!

If it’s not causing any problems, why change 😄
