Ruby on rails 在jbuider、Ruby on Rails中显示之前检查哈希值

Ruby on rails 在jbuider、Ruby on Rails中显示之前检查哈希值,ruby-on-rails,ruby,jbuilder,Ruby On Rails,Ruby,Jbuilder,我有一个jbuilder来显示我的对象 这就是它的样子(部分) 接下来,我将看到: { features: { pool: { params: null, present: "false" }, garage_parking: { params: null, present: "false" }, terrace_balcony: { params: null, present: "true" } } } 问题是我不想在输出

我有一个jbuilder来显示我的对象

这就是它的样子(部分)

接下来,我将看到:

{
 features: {
  pool: {
   params: null,
   present: "false"
   },
  garage_parking: {
   params: null,
   present: "false"
   },
  terrace_balcony: {
   params: null,
   present: "true"
   }
 }
}
问题是我不想在输出特性中显示present:“false”。为了解决这个问题,我编写了下一个代码

json.features o.property_object_feature_pack, :pool if o.property_object_feature_pack.pool['present'] === "true"
json.features o.property_object_feature_pack, :garage_parking if o.property_object_feature_pack.garage_parking['present'] === "true"
json.features o.property_object_feature_pack, :terrace_balcony if o.property_object_feature_pack.terrace_balcony['present'] === "true"
它确实有效,但看起来很糟糕。如何重构它

下一个问题是,将来可以在ActiveRecord中添加新功能,我想知道我们可以在不设置名称的情况下迭代o.property\u object\u feature\u pack吗?(:游泳池、车库、停车场等)

ps.原始o.property\u object\u feature\u pack

features: {
 id: 820,
 property_object_id: 56879,
 created_at: "2015-04-27T18:25:25.712Z",
 updated_at: "2015-04-27T18:25:25.712Z",
 pool: {
  params: null,
  present: "false"
 },
 garage_parking: {
  params: null,
  present: "false"
 },
 terrace_balcony: {
  params: null,
  present: "true"
 },
 av_apartments: {
  params: null,
  present: "false"
 }
}
已更新

受#mudasobwa的启发,我创建了一个小助手方法来解决我的问题

def features_list
    @property_object.property_object_feature_pack.as_json.select! do |_, v|
      v.is_a?(Hash) && v['present'] == 'true'
    end
  end

我不知道什么是
jbuilder
,但我建议将json转换为纯ruby哈希,做任何你想做的事情,然后将其转换回json:

require 'json'
hash = JSON.parse what_you_got_from_builder
hash[:features].select! do |_, v| 
  !v.is_a?(Hash) || v[:present] == 'true' 
end
hash.to_json

不完全回答,但你把我推到正确的方向。最后,我创建了一个助手,可以做我需要的事情<代码>def features_list@property_object.property_object_feature_pack.as_json.select!do | |,v | v.is|u a?(散列)和&v['present']='true'结束
require 'json'
hash = JSON.parse what_you_got_from_builder
hash[:features].select! do |_, v| 
  !v.is_a?(Hash) || v[:present] == 'true' 
end
hash.to_json