1

在 Rails 4.0 后端我有以下内容:

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

我想将用户的好友列表传入 JSON,所以我写了一个序列化器:

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

在余烬方面,我正在尝试使用以下User模型加载 JSON:

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

但这给了我一个错误

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

它要求我提供Friend模型,但 aFriendUser. 我猜我需要告诉 ember-data 该friends数组实际上是一个User记录数组,但如果friends: DS.hasMany('user')不这样做,那我不​​知道该怎么做。如何将此数据结构正确加载到 ember 中?

4

1 回答 1

1

在 AMS 中,您可以指定根,在您的情况下,您的朋友关系的根将是用户,这样的事情应该可以工作

has_many :friends, include: true, root: :users
于 2014-01-13T13:06:33.077 回答