Calling related models in model

41 views
Skip to first unread message

Andrew

unread,
Nov 18, 2015, 3:21:41 PM11/18/15
to LoopbackJS
I was curious as to what the accepted way to call related models is within the model file. In keeping with the Customers hasMany Orders example, it appears we must do the following:

var orderModel = Customer.app.models.Order;
orderModel .create( { customerId : <yourid>});

Is it possible to use the prototype.__create__customers() function, or is this only applicable within the server.js / boot scripts? I'm attempting to find the best way to force the creation of model B upon creation of model A, and ran into this question...

Andrew

unread,
Nov 23, 2015, 9:51:17 AM11/23/15
to LoopbackJS
Sorry to bump this, I know it's probably a very obvious question to some..

Líus Fontenelle Carneiro

unread,
Nov 23, 2015, 12:33:18 PM11/23/15
to LoopbackJS
Hi Andrew,

You can work with models from anywhere, following some rules[1].

To create a record of ModelB when ModelA is created, you can create an after save operation hook[2] on ModelA and create the new record invoking some method of ModelB to do this, like create (passing the foreign key you need) or a relation method[3], that set the key for you directly on calls made on ModelA instance.

Best Regards,

Líus


Reply all
Reply to author
Forward
0 new messages