How to display all jbuilder attributes without specifying all of them?

I use json to store versions of my data in postgresql. I would like to output a whole tree of objects with children, children of children, etc. And all the attributes. If any attributes are added to any of the objects later, I would like them to be included in the next json.

Is there a way to display all the content without using each attribute? those. not this way:

json.(object_name, :id, :attr1, :attr2.... etc) 
+4
source share
3 answers

I know this is an old thread, but I was interested in the same thing, and I ended up here. Then I found a great answer here => How to extract all attributes using Rails Jbuilder?

@uiureo suggests using json.merge! and it worked for me :)

 json.merge! object_name.attributes 
+7
source

If you want your json to come out like this:

 {"id":1,"attribute1":1,"attribute2":2} 

You can do it:

 json.array! @my_object 

However, if you need an output that looks like this:

 {"my_object":{"id":1,"attribute1":1,"attribute2":2}} 

You can do it:

 json.my_object @my_object 
+1
source

You can look at json.except!

json.except! @resource, :id, :updated_at

json.except! @resource

https://github.com/chenqingspring/jbuilder-except

0
source

Source: https://habr.com/ru/post/1496010/


All Articles