Question

In Rails 4.0 backend I have the following:

class User < ActiveRecord::Base
  has_many :friendships, dependent: :destroy
  has_many :friends, through: :friendships

class Friendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, class_name: User
end

I want to pass the user's friend list into JSON, so I write a serializer:

class UserSerializer < ActiveModel::Serializer
  embed :ids, include: true
  has_many :friends, include: true

On the ember-side, I'm trying to load the JSON with the following User model:

Nektere.User = DS.Model.extend
  friends: DS.hasMany('user')

but this gives me an error

Assertion failed: No model was found for 'friend'
Uncaught TypeError: Cannot set property 'typeKey' of undefined 

It's asking me for a Friend model, but a Friend is a User. I'm guessing I need to tell ember-data that the friends array is actually an array of User records, but if friends: DS.hasMany('user') doesn't do it, then I don't know how. How do I load this data structure properly into ember?

Was it helpful?

Solution

In AMS you can specify the root, in your case the root for your friends relationship would be users, something like this should work

has_many :friends, include: true, root: :users
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top