我需要呈現爲Json一個複雜的結構。我有一個結構的工作:Rails 4呈現json嵌套對象
render :json => @booking, :include => [:paypal,
:boat_people,
:boat => {:only => :boat_model, :include => {:boat_model => {:only => :name, :include => { :boat_type => {:only => :name}}}}}]
但我可不是能夠添加端口屬性與其他一些嵌套的屬性:船,如:boat_model(在同一水平)。
UPDATE:
雖然it's不工作,我有我的端口屬性。
render :json => @booking, :include => [:paypal,
:boat_people,
:boat => {:only => [:boat_model => {:include => {:boat_model => {:only => :name, :include => { :boat_type => {:only => :name}}}}},
:port => {:include => :city => {:only => name}}]}]
我的意思是,boat_model和port都是船屬性。
這是模型對象:
class Boat < ActiveRecord::Base
attr_accessor :price
@price
attr_accessor :extrasPrice
@extrasPrice
def as_json(options = { })
h = super(options)
h[:price] = @price
h[:extrasPrice] = @extrasPrice
h
end
belongs_to :boat_model
belongs_to :port
belongs_to :state
has_many :photos
end
你可以包括你嘗試過爲了添加具有嵌套屬性的端口嗎? 另外,你周圍Port的關係是什麼樣的? –
@Joe Edgar查看更新。 – Rober