Ho bisogno di rendere come Json una struttura complessa. Ho successiva struttura di lavoro:Rails 4 render jested nested objects
render :json => @booking, :include => [:paypal,
:boat_people,
:boat => {:only => :boat_model, :include => {:boat_model => {:only => :name, :include => { :boat_type => {:only => :name}}}}}]
ma io non sono in grado di aggiungere un attributo porto con alcuni altri attributi nidificati a: barca, come ad esempio: boat_model (allo stesso livello).
UPDATE:
Anche se E'non funziona, includo il mio attributo porta.
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}}]}]
Voglio dire, boat_model e port sono entrambi attributi della barca.
Questo è l'oggetto del modello:
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
puoi includere ciò che hai provato per aggiungere Port con i suoi attributi nidificati? Inoltre, che aspetto hanno le tue relazioni intorno a Port? –
@Joe Edgar vede l'aggiornamento. – Rober