1

에서 레일 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') 

그러나 이것은 나에게 그것은 Friend 모델 나에게 묻는 데요 오류

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

을 제공하지만, FriendUser이다. 나는 friends 배열이 실제로 User 레코드의 배열이라는 ember-data를 말할 필요가 있다고 생각하지만, friends: DS.hasMany('user')이 그것을하지 않는다면, 나는 어떻게해야할지 모른다. 이 데이터 구조를 ember에 제대로로드하려면 어떻게합니까? 당신이 사용자가 될 친구 관계를 귀하의 경우, 루트 루트를 지정할 수 있습니다 AMS에서

답변

1

,이 같은 응답에 대한

has_many :friends, include: true, root: :users 
+1

감사를 작동합니다! 이것은'user' 객체 내의'friend_ids '에 해당하는 JSON에서'users' 배열을 사이드로드하는 것으로 보입니다. ember-data가 올바르게 읽는 것 같지만 좀 더 철저하게 테스트해야합니다. 예를 들어': friends'와': followers'와 같이 두 개의 별개의 사용자 배열을 사이드로드해야 할 경우 어떻게 작동하는지 궁금합니다. 두 배열을 모두'사용자 '라고 지정할 수는 없습니까? – xph