-
Notifications
You must be signed in to change notification settings - Fork 108
How to use ToOneRelationship() and serializeAs() on the same attribute? #175
Comments
@gaetanm why do you need to apply both attributes on the same property? class Menu: Resource {
var nbrOfRecipes: Int?
var userID: String?
var user: User?
|
Perhaps you could provide an example of your response JSON to make it clear? |
I don't know enough about how Spine works internally to know that it was possible to use 2 properties for the same JSON data. Interesting! About the JSON response, it's like this: "data": {
"id": "4",
"type": "menus",
"attributes": {
"nb-recipes": 4,
"created-at": "2016-11-27 19:50:07",
"user-id": 1
} By using 2 properties like you showed me, I now have an assertion failure:
My query is: var query = Query(resourceType: Menu.self)
query.whereAttribute("user-id", equalTo: userID!) My class is now: class Menu: Resource {
var nbrOfRecipes: Int?
var user: User?
var userID: String?
var recipes: LinkedResourceCollection?
override class var resourceType: ResourceType {
return "menus"
}
override class var fields: [Field] {
return fieldsFromDictionary([
"nbrOfRecipes": Attribute().serializeAs("nb-recipes"),
"userID": Attribute().serializeAs("user-id"),
"user": ToOneRelationship(User.self),
"recipes": ToManyRelationship(Recipe.self)
])
}
} It's really a black box for me. Do I have this error because of those 2 properties? |
to resolve the error:
query.whereAttribute("userID", equalTo: userID!) Spine will perform the appropriate transform/formatting from property name to field name as described in |
@gaetanm please see examples on http://jsonapi.org/ for how "data": [{
"id": "4",
"type": "menus",
"attributes": {
"nb-recipes": 4,
"created-at": "2016-11-27 19:50:07",
},
"relationships": {
"user": {
"data": { "type": "users", "id": "1" }
},
}
}],
"included": [{
"type": "users",
"id": "1",
"attributes": {
"first-name": "Dan",
"last-name": "Gebhardt",
}, Which includes the Without needing the additional |
Thanks, it's more clear now. I have a last error message:
No clue about why I have that error. I already had a similar error message in the past but it was caused by the storyboard, which seems to not be the case here. |
@gaetanm |
You will understand just by looking at my class:
As you can guess, this result in a
fatal error: Dictionary literal contains duplicate keys
.How to do to apply both
ToOneRelationship(User.self)
andAttribute().serializeAs("user-id")
on the same attribute?The text was updated successfully, but these errors were encountered: