Model Associations


#1

I’ve never been quite clear on this. In an ember app built on a rails api backend do you set up all the model associations – has_many, belongs_to, etc. on the rails end, ember end or both? Does this change depending on whether one is using Ember Data or the object model, or some other persistence layer?


#2

Ultimately, it is up to you to setup your backend. The Ember app will deal with the data that is provided. The requirement is that you need to serve up JSON. How exactly you create that JSON is ultimately the problem your backend app has to solve.

There are some conventions to follow that will make this go smoother.

If you are using Rails you should try using

Active Model Serializers

This will have the benefit of packaging up the JSON in a format that is useful for Ember apps. For more technical detail on the way your JSON should be organized checkout the

JSON API Spec

http://jsonapi.org

From what I can tell the essential characteristic of successfully linking Ember to a Rails backend is to have a series of meaningful end points or specific URLs that load the data expected by the models.

When Ember data and some of the other persistence frameworks attempt to communicate to the server they will access end points based on the model name.

So if you have a model in your ember app called

App.Product

That means call to

App.Product.find()

Wil make a call to

/products

Which is a GET request to the index action on your controller. It is up to you to define the specific semantics of what this should return as a collection.

App.Product.find(params.product_id)

Will take the value of params id and pass it to the “show” action of your rails resource.

/products/1
/products/2
/products/3
/products/some_id_string

etc.

When you pass arbitrary parameters to the find method, for example:

App.Product.find({foo:"foovalue", bar:"barvalue" })

These will create query string parameters in your GET request. e.g.

/products?foo=foovalue&bar=barvalue

Again up to you to implement the specific semantics of this, and how to respond to foo and bar key/value pairs in your rails action.

You can customize much of this behavior to meet whatever specific needs your backend requires. But there is definitely a so called “happy path” by following the conventions defined on the JSONAPI.org site and using tools like active model serializer.

Short answer is yes it will help to mirror the hasMany and belongsTo relationships on both the rails side and the ember model side. Ultimately your controller logic can dictate the format of the JSON output if you need to do something custom or not idiomatic.

When debugging this stuff use the network tab of the chrome debug console so see the XHR requests being sent to the server. Always good to spot check that the JSON return makes sense for your app.

On the Ember side of things it uses an “identity map” locally to tie the models together. So all the individual records are bound to each other via primary id fields. This will behave in a way similar to what you are familiar with ActiveRecord.

All of the above assumes you are using the RESTAdapater.

But you can also implement custom adapters if needed.

Anyway, hope that helps.

Also, Dan Gebhardt has a good presentation on this topic. Was very helpful to me when I was first trying to understand this.

Video

Slides:

Notes:

http://cgcardona.github.io/2013/02/15/notes-from-the-optimizing-your-api-for-ember-data-session/


#3

Awesome post! Thanks so much, this is extremely helpful.


#4

No problem, glad to help. Trying to explain it is helping me to cement it in my own mind. Last few months has a been a slog of learning as much as I can about the framework.