Saving hasMany for existing objects


#1

I’m learning ember by writing a small video catalog app.

The models look like so:

export default DS.Model.extend({

    name:        DS.attr('string'),
    year:        DS.attr('number'),
    categories:    DS.hasMany('category')
});

and

export default DS.Model.extend({    
    name:     DS.attr('string'),
    videos: DS.hasMany('video')
});

I have created forms for capturing new categories and new videos.

For the new videos form, I use a multiselect-checkbox to enable the user to select the categories the title belongs to. These are pulled from the model in the videos/new route:

export default Ember.Route.extend({
    model: function() {
        return this.store.findAll('category');
}});

I’m not really sure how I should be saving the category along with the video - at the moment my videos/new createVideo function looks like this:

createVideo: function() {

    // movieCategories map stores the category.id => category.name

    let newVideo = this.store.createRecord('video', {name: this.get('name'), year: this.get('year') });

    let cats = newVideo.get('categories');

    for (var key in this.get('movieCategories'))
    {

        let rec = this.store.findRecord('category', {id: key.toString()});

        if (rec) { cats.pushObject(rec); }
    }

    newVideo.set('categories', cats);
    newVideo.save();

}

I can’t seem to find any documentation or examples on this, any links or explanations greatly appreciated.


#2

Hi, I’m also new as you are. I waited 1 day to answer because I hoped someone else would have a good answer for you. I have very similar questions to the ones you ask and am also new to Ember. The main concern for me isn’t how to do stuff like this but rather how to do it well and to get good results even as the application scales. I think your concerns are similar to mine?

I’m thinking that what your doing seems natural from a relational standpoint. You built objects and linked them using .hasMany. That seems to be what the docs suggest doing. My concern would be, does linking the videos from the category make the category a giant object every time we need to query it? And the answer I’m 95% certain is no, as long as the hasMany relationship is async then your 'category' object would come back from the database with only the name property resolved to a string and the videos property would come back with just the IDs rather than fully resolved objects (meaning every linked video attached in full before you could even use the model). If the relationship was syncronous then anytime the category was queried it would have to wait on every single video attached to it whether or not that video was relevant to the page.

I think we are using Ember at a good time because before Ember 2.0 hasMany models were {async: false} by default, now they are true by default meaning that the lightest possible model is returned from the database by default when using hasMany as-is.

I’m not sure if I addressed your exact concerns, those were the concerns I had been thinking about and when I read your question I felt like other people might be having the same doubts as to what the correct way to do these things are.

Here is a screencast (Dec 2015) discussing hasMany relationships and what async does. https://www.emberscreencasts.com/posts/104-ember-data-20-relationships

Here is an article about nested models in Ember which also addresses hasMany relationships on a deeper level than the screencast above. (Jan 2016), http://thejsguy.com/2016/01/29/working-with-nested-data-in-ember-data-models.html

Any Ember gurus who have real experience with building NoSQL relationships and knows well the caveats that come over time I would be interested and I think @Chancer probably would appreciate your input as well. Thanks in advance!.

:smile: