Ruby on rails json对象的过滤器数组
我有以下json对象数组Ruby on rails json对象的过滤器数组,ruby-on-rails,ruby,arrays,json,filter,Ruby On Rails,Ruby,Arrays,Json,Filter,我有以下json对象数组 [{"template":{"title":"title 1"}},{"template":{"title":"title 1"}},{"template":{"title":"title1111"}},{"template":{"title":""}},{"template":{"title":""}},{"template":{"title":"this is the title"}},{"template":{"title":"title232"}},{"temp
[{"template":{"title":"title 1"}},{"template":{"title":"title 1"}},{"template":{"title":"title1111"}},{"template":{"title":""}},{"template":{"title":""}},{"template":{"title":"this is the title"}},{"template":{"title":"title232"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"title with msg"}},{"template":{"title":"this is a title"}},{"template":{"title":"this is a title"}},{"template":{"title":"this is a title"}},{"template":{"title":"some title"}},{"template":{"title":"some title"}},{"template":{"title":"some title"}},{"template":{"title":"some title"}}]
我希望这个数组是这样的
[{'title': 'some title'}, {'title': 'some other title'}, ...]
所以基本上我不想在js响应中使用template属性。
我是从下面的rails代码中得到的
@templates = Template.find_all_by_user_id(current_user.id, :select=>'title', :conditions=>"title is not null")
@templates.to_json
就是这个json对象数组的来源
有什么建议吗?我认为在这种情况下,您可以只收集整个
模板集合的属性:
@templates = Template.find_all_by_user_id(current_user.id, :select=>'title', :conditions=>"title is not null").collect(&:attributes)
(向右滚动查看行末添加的.collect(&:attributes)
。检查:
Template.include_root_in_json = false