2017-02-14 98 views
1

我有三种模式,并试图在单个对象中将它们全部输出为JSON。是返回多个包含在对象上的Ruby on Rails 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 

地址和订阅需要在阵列中被传递:)