ruby - Rails to_json belongs_to object -


i have 2 models: cabinet , workplace.

class cabinet < activerecord::base    def as_json(options={})     options.merge!({except: [:created_at, :updated_at]})     super(options)   end  end  class workplace < activerecord::base   belongs_to :cabinet    def as_json(options = {})     options.merge!(:except => [:created_at, :updated_at, :cabinet_id], include:  :cabinet)     super(options)   end  end 

when called cabinet.first.to_json

{     id: 1,      cabinet: "100" } 

but when called workplace.first.to_json id

{   name: "first workplace",   cabinet: {            id: 1,             cabinet: "100",            created_at: "#created_at",            updated_at: "#updated_at"            } } 

why this? , sorry english :)

not sure if following you, want attributes workplace model, , not cabinet data when workplace.first.to_json?

i think because include cabinet in as_json method configuration explained here.

you should either remove or this:

workplace.first.attributes.to_json 

let me know if missing question.


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -