在 jbuider 中显示之前检查散列值,Ruby on Rails
Checking values of hash before show in jbuider , Ruby on Rails
我有一个 jbuilder 来显示我的对象。
这就是它的样子(部分)
o = @property_object
json.features o.property_object_feature_pack, :pool, :garage, terrace
接下来显示的是:
{
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_object_feature_pack不设置名称吗? (:pool, :garage_parking 等..)
ps. 原始 o.property_object_feature_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
我有一个 jbuilder 来显示我的对象。
这就是它的样子(部分)
o = @property_object
json.features o.property_object_feature_pack, :pool, :garage, terrace
接下来显示的是:
{
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_object_feature_pack不设置名称吗? (:pool, :garage_parking 等..)
ps. 原始 o.property_object_feature_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