2017-02-14 10 views
1

私は3つのモデルを持っており、それらをすべて単一オブジェクト内のJSONとして出力しようとしています。次のようにオブジェクトに複数のインクルードを戻すJSON

モデルや団体は、次のとおりです。

class Customer < ApplicationRecord 
    has_one :subscription 
    has_one :address 
end 

class Address < ApplicationRecord 
    belongs_to :customer 
end 

class Subscription < ApplicationRecord 
    belongs_to :customer 
end 

私は、JSONは以下を使用して関連するすべてのデータを返すようにしようとしています:

class HomeController < ApplicationController 
    def index 
    @customers = Customer.all 
    render json: @customers, :include => :address, :subscription 
    end 
end 

しかし、これはどこの構文エラーを返してルビーはどこかで=>を期待しています。どのように私はこのデータのすべてを出力することができます上の任意の助けが大歓迎です。

答えて

1

誰かが探しています私は次の構文でこの質問に答えました。

配列で渡すことが必要
class HomeController < ApplicationController 
    def index 
    @customers = Customer.all 
    render json: @customers, :include => [:address, :subscription] 
    end 
end 

アドレスおよびサブスクリプション:)

関連する問題