             if !relationshipDescription.toMany {
                 
                 // get destination resource
-                let destinationResource = managedObject.valueForKey(relationshipName) as NSManagedObject
-                
-                // get resource ID
-                let destinationResourceID = destinationResource.valueForKey(self.resourceIDAttributeName) as UInt
-                
-                // add to JSON object
-                jsonObject[relationshipName] = destinationResourceID
-            }
-                
+                if let destinationResource = managedObject.valueForKey(relationshipName) as? NSManagedObject {
+                    
+                    // get resource ID
+                    let destinationResourceID = destinationResource.valueForKey(self.resourceIDAttributeName) as UInt
+                    
+                    // add to JSON object
+                    jsonObject[relationshipName] = destinationResourceID
+                }
+            }
+            
             // to-many relationship
             else {
                 
-                // get destination collection
-                let toManyRelationship = managedObject.valueForKey(relationshipName) as [NSManagedObject]
-                
-                // get resource IDs
-                var resourceIDs = [UInt]()
-                
-                for destinationResource in toManyRelationship {
-                    
-                    let destinationResourceID = destinationResource.valueForKey(self.resourceIDAttributeName) as UInt
-                    
-                    resourceIDs.append(destinationResourceID)
-                }
-                
-                // add to jsonObject
-                jsonObject[relationshipName] = resourceIDs
+                if let arrayValue = managedObject.arrayValueForToManyRelationship(toManyRelationship: relationshipName) {
+                    
+                    // get resource IDs
+                    var resourceIDs = [UInt]()
+                    
+                    for destinationResource in arrayValue {
+                        
+                        let destinationResourceID = destinationResource.valueForKey(self.resourceIDAttributeName) as UInt
+                        
+                        resourceIDs.append(destinationResourceID)
+                    }
+                    
+                    // add to jsonObject
+                    jsonObject[relationshipName] = resourceIDs
+                }
             }
         }
         

internal extension NSManagedObject {
    
    /** Get an array from a to-many relationship. */
    func arrayValueForToManyRelationship(toManyRelationship key: String) -> [NSManagedObject] {
        
        // assert existence of relationship
        assert(self.entity.relationshipsByName[key] as? NSRelationshipDescription != nil, "Relationship \(key) doesnt exist on \(self.entity.name)")

                else {
                    
                    // only add if value is present
                    if let arrayValue = managedObject.arrayValueForToManyRelationship(toManyRelationship: relationshipName) {
                        
                        // only add resources that are visible
                        var resourceIDs = [UInt]()
