1

I just wanted to check that there was nothing wrong with a model having 2 belongsTo:

@belongsTo 'claim'
@belongsTo 'buyer'

I ask because I have this and saving does not produce the json it should:

create: =>
    alert @claim
    CT.Buyer.find $("#buyer_id").val(), (err, buyer) =>
        @bid.set 'claim', @claim
        @bid.set 'buyer', buyer
        @bid.save()

    return false

The alert @claim shows me clearly that @claim is correct and contains the Claim I expect. But the json sent when save() is called looks like:

{buyer_id:52c86c74-2425-11e1-8b23-0021cc5da1e1, amount:123}

It is not sending claim_id for some reason.

2 Answers2

1

It's fine to have multiple belongsTo associations on a model. Are you sure the buyer you are trying to find exists? You should always handle err inside find callbacks.

ktusznio
  • 3,585
  • 3
  • 22
  • 21
0

Are you encode-ing claim_id in your model code? Batman.Model won't send back values in it's JSON unless you use encode. For example:

class App.Bid extends Batman.Model
  @belongsTo 'claim'
  @belongsTo 'buyer'
  @encode 'claim_id', 'buyer_id'

You can also use the encodeForegnKey option:

class App.Bid extends Batman.Model
  @belongsTo 'claim', encodeForeignKey: true
  @belongsTo 'buyer', encodeForeignKey: true
rmosolgo
  • 1,854
  • 1
  • 18
  • 23