maccman/spine — Issue #172: Loading models with a hasOne relationship
Author: mitchlloyd
Created: 2011-12-30 16:26:06+00:00
State: open

I have a document model which @hasOne() workflow. Let's say I do a Document.fetch() and I load in a document with an embedded workflow:

{id: "ABCD-12345", name: "My Doc Name", workflow: {name: "My Workflow Name"}}

I would expect functionality like @hasMany() where Spine loads the embedded record and triggers a refresh. However the Singleton update method is called [line 78; relation.coffee](https://github.com/maccman/spine/blob/master/src/relation.coffee#L78) which creates a model from JSON and then saves it as a new record.

To show you the behavior I'm expecting, I pasted a method I wrote to replace the workflow() method created by @hasOne() below.

```coffeescript
workflow: (attr) ->
if attr
# Set the fkey and perform a refresh with the provided record.
attr.documentId ?= this.id
Workflow.refresh(attr)
else
# Should just perform a find()
this.id and Workflow.findByAttribute('documentId', this.id)
```
The current way a hasOne relation works would allow someone to save an embedded document by calling workflow(attr), but my method doesn't allow this.

How is fetching a model with an embedded model supposed to work with @hasOne()? I'd be happy to work on a patch but I want to make sure I'm not missing something.