Real purpose of belongsTo (hasMany)


#1

Hi!

I’m for sure doing something wrong, but maybe somebody can clarify it. Thanks in advance! So let we say i have two simple models user -> { id:attr(), role:DS.belongsTo(‘role’’), desc:attr() } and role -> { id:attr(), label:attr() } Everything works fine for display. Instead of role id I can show it’s related label. But when it comes to update or save - it’s real pain (at least for me :slight_smile: ) I can`t use (mut (get … )) anymore? Every time I need handle this via actions and do something like user.set(‘role’, store.peekRecord(‘role’, id)); When in case of primitive attributes (mut (get …)) works fine.

So. Instead of using belongsTo I can use computed props and it will look like user -> { id:attr(), role:attr(), roleLabel:computed(‘role’,function() { return this.store.findRecord(‘role’,this.get(‘role’)); }, desc:attr() } I still able to display role label, and i can use (mut (get … )) because it’s computed prop now. Less pain to save data in db ( it`s pretty much simple db row user, role, desc where role just a number).

The same story with hasMany.

So what is real purpose of using belongsTo? What i’m doing wrong? Please help! :slight_smile:


#2

Well, your computer property just reimplemented DS.belongsTo. That’s pretty much exactly how it works.