开发者

Reducing the depth of a Hash object

开发者 https://www.devze.com 2023-01-04 04:11 出处:网络
I have a hash object from an ActiveRecord.I\'m going to be calling to_json on it, but I am looking to reduce the depth of the object that is being returned.Here is what my find looks like:

I have a hash object from an ActiveRecord. I'm going to be calling to_json on it, but I am looking to reduce the depth of the object that is being returned. Here is what my find looks like:

@tags = Tag.find_all_by_type("some_type", :select => :name)

The result of @tags.to_json looks like this:

[{"tag": {"name": "some tag name"}},
 {"tag": {"name": "another tag name"}},
 {"tag": {"name": "etc..."}}]

However, I want the result to look like this since I don't need each object wrapped 开发者_如何转开发in a tag object:

[{"name": "some tag name"}, {"name": "another tag name"}, {"name": "etc..."}]

Is there a way I can do this through a map, collect, or similar call?


If you set the variable ActiveRecord::Base.include_root_in_json to false, you will get the behavior you are looking for.

Reference

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号