Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to connect Rails Grape API with Ember reflexive model

I'm having a problem with connecting Json payload from Grape (serialized with Grape entity) with Ember reflexive model. Model looks like this:

Category = DS.Model.extend {
  name: DS.attr 'string',
  children: DS.hasMany 'category', inverse: 'parent',
  parent: DS.belongsTo 'category', inverse 'children'
}

So as you can see I'm trying to model here category-subcategory relation. Example json response from endpoint is:

{
  "category": {
    "id": 1,
    "name": "Sport",
    "child_ids": [
      5,
      6,
      8,
      7
    ]
  },
  "children": [
    {
      "id": 5,
      "name": "Basketball",
      "parent_id": 1
    },
    {
      "id": 6,
      "name": "Football",
      "parent_id": 1
    },
    {
      "id": 8,
      "name": "Running",
      "parent_id": 1
    },
    {
      "id": 7,
      "name": "Volleyball",
      "parent_id": 1
    }
  ]
}

There is Warning message:

WARNING: Encountered "children" in payload, but no model was found for model name "child" (resolved model name using mateby-front@serializer:-active-model:.modelNameFromPayloadKey("children"))

As it says in documentation for ActiveModelAdapter it should provide ids for nested resource and id for parent resource with sideloaded data. The problem is that there is one category record in ember inspector but without related children.

I've tried also to make two models: category and subcategory and provide same payload with other ids/id namings and I could see that in this situation there were all required Subcategory records but not as hasMany for Category (two seperated data).

like image 835
szsoppa Avatar asked Nov 10 '22 04:11

szsoppa


1 Answers

The problem may come from the fact you forget to put double dot in front of your second inverse :

change

Category = DS.Model.extend {
  name: DS.attr 'string',
  children: DS.hasMany 'category', inverse: 'parent',
  parent: DS.belongsTo 'category', inverse 'children' #here
}

to

Category = DS.Model.extend {
  name: DS.attr 'string',
  children: DS.hasMany 'category', inverse: 'parent',
  parent: DS.belongsTo 'category', inverse: 'children' #here
}
like image 77
Nathan Gouy Avatar answered Nov 15 '22 04:11

Nathan Gouy