0

我正在使用Rails 5 api only app。當我嘗試通過Rails返回多個模型的散列時,活動模型序列化器不工作?

所以這是我的模型序列

class MovieSerializer < ActiveModel::Serializer 
    attributes :id ,:name,:release_year,:story,:in_theater,:poster,:score,:user_count 

    belongs_to :age_rating 
    belongs_to :company 
    has_many :category , through: :movie_category 

end 

class CelebritySerializer < ActiveModel::Serializer 
    attributes :id, :first_name, :last_name 

    has_many :movie_celebrity 

end 

class MovieCelebritySerializer < ActiveModel::Serializer 
    attributes :id,:vacancy,:title 

    belongs_to :movie 
    belongs_to :celebrity 

end 

這是我的控制器

class Api::V1::MoviesController < ApplicationController 

    # GET /v1/movies/:id 
    def show 

    movie = Movie.find_by(id: params[:id]) 
    casts = MovieCelebrity.where(movie_id: params[:id],vacancy: "cast") 
    directors = MovieCelebrity.where(movie_id: params[:id],vacancy: "director") 

    render :json => {:movie => movie, :casts => casts , :directors => directors} 
    end 

end 

所以這是我得到了什麼,當我提出的要求

{ 
    "movie": { 
    "id": 1, 
    "name": "0 The Doors of Perception", 
    "release_year": 2007, 
    "story": "Non doloribus qui et eum impedit. Rerum mollitia debitis sit nesciunt. Vero autem quae sit aliquid rerum ex fugit. Eligendi assumenda et eos. Blanditiis hic ut. Commodi quo sunt voluptatem quasi.", 
    "in_theater": false, 
    "age_rating_id": 2, 
    "company_id": 5, 
    "created_at": "2016-10-12T12:45:26.213Z", 
    "updated_at": "2016-10-12T12:45:26.213Z", 
    "release_date": "2016-01-18", 
    "poster": "'http://cdn.mos.cms.futurecdn.net/15399e7a7b11a8c2ef28511107c90c6f.jpg',", 
    "score": 0, 
    "user_count": 6950 
    }, 
    "casts": [ 
    { 
     "id": 2, 
     "vacancy": "cast", 
     "title": "Pro x", 
     "movie_id": 1, 
     "celebrity_id": 56, 
     "created_at": "2016-10-12T12:45:28.001Z", 
     "updated_at": "2016-10-12T12:45:28.001Z" 
    }, 
    { 
     "id": 3, 
     "vacancy": "cast", 
     "title": "Magneto", 
     "movie_id": 1, 
     "celebrity_id": 23, 
     "created_at": "2016-10-12T12:45:28.006Z", 
     "updated_at": "2016-10-12T12:45:28.006Z" 
    } 
    ], 
    "directors": [ 
    { 
     "id": 1, 
     "vacancy": "director", 
     "title": "", 
     "movie_id": 1, 
     "celebrity_id": 17, 
     "created_at": "2016-10-12T12:45:27.993Z", 
     "updated_at": "2016-10-12T12:45:27.993Z" 
    } 
    ] 
} 

好像活躍模型序列化器沒有工作。 因爲如果我只返回一個對象,它就像這樣工作得很好。

def show 

    movie = Movie.find_by(id: params[:id]) 
    casts = MovieCelebrity.where(movie_id: params[:id],vacancy: "cast") 
    directors = MovieCelebrity.where(movie_id: params[:id],vacancy: "director") 

    #render :json => { :movie => movie, :casts => casts , :directors => directors } 
    render :json => movie 

end 



{ 
    "id": 1, 
    "name": "0 The Doors of Perception", 
    "release_year": 2007, 
    "story": "Non doloribus qui et eum impedit. Rerum mollitia debitis sit nesciunt. Vero autem quae sit aliquid rerum ex fugit. Eligendi assumenda et eos. Blanditiis hic ut. Commodi quo sunt voluptatem quasi.", 
    "in_theater": false, 
    "poster": "'http://cdn.mos.cms.futurecdn.net/15399e7a7b11a8c2ef28511107c90c6f.jpg',", 
    "score": 0, 
    "user_count": 6950, 
    "age_rating": { 
    "id": 2, 
    "name": "PG" 
    }, 
    "company": { 
    "id": 5, 
    "name": "Gislason, Jacobs and Graham" 
    }, 
    "category": [ 
    { 
     "id": 4, 
     "name": "Biography" 
    }, 
    { 
     "id": 16, 
     "name": "Mystery" 
    } 
    ] 
} 

我該如何解決這個問題,並使活動模型序列化工具正常工作與多個模型哈希對象?

謝謝!

回答

0

您需要設置explicit serializer這樣的:

render json: {movie: movie, casts: casts , directors: directors}, serializer: MovieCastDirectorSerializer 

因爲AMS不知道哪個串行需要你建立你在這裏平原紅寶石對象。

更新: 但即使你這樣做,AMS仍然將無法序列化,因爲它將不是ActiveModel對象。 There is an example如何處理。但對我個人來說,這似乎太像一個黑客。我認爲你不應該太早濫用REST,並且應該將序列化程序與實際結束點分開,並且只有嵌套在實際結構中才能嵌套資源。因爲隨着代碼的成熟,很難告訴你在哪些位置獲得顯示的數據。

+0

我怎樣才能將我所有的串行器合併到一個'(MovieCastDirectorSerializer)'? – user3403614

+0

通過在形式has_many:movies,has_many:casts,has_many:director中定義這樣的序列化器。 –

+0

順便說一句,像'through:'這樣的東西在AMS級別上沒有意義。只是被告知要將收藏品系列化,無論它是從哪裏分配的。 –