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

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -