我已经编写了小型ruby代码来获取发布统计信息,代码正在跟随
begin
response = conn.get("api/queues")
statistics = JSON.parse(response.body)
statistics.each do |qDetails|
payload = "#{qDetails["name"]}"
if payload != "aliveness-test"
puts "#{qDetails["message_stats"]["publish"]}"
end
end
rescue Faraday::Error::ConnectionFailed => e
puts "Connection failed"
end
但是我得到了这个错误
undefined method `[]' for nil:NilClass (NoMethodError)
message_stats json就像这样
{"deliver_get_details"=>{"rate"=>0.0}, "deliver_get"=>1357, "ack_details"=>{"rate"=>0.0}, "ack"=>1357, "redeliver_details"=>{"rate"=>0.0}, "redeliver"=>0, "deliver_no_ack_details"=>{"rate"=>0.0}, "deliver_no_ack"=>0, "deliver_details"=>{"rate"=>0.0}, "deliver"=>1357, "get_no_ack_details"=>{"rate"=>0.0}, "get_no_ack"=>0, "get_details"=>{"rate"=>0.0}, "get"=>0, "publish_details"=>{"rate"=>0.0}, "publish"=>1400}
问题是什么?
答案 0 :(得分:2)
对于红宝石> 2.3,您可以使用@JoinColumn
:
@Entity
public class OrderPosition implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@ManyToOne
@JoinColumn(name = "order_id", referencedColumnName = "id")
//@Json???
private Order order;
// ...
}
orderPosition = new OrderPosition()
// ...
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(orderPosition);
assertThat(json).contains("\"order_id\":");
这将安全地访问//currently selected company
$currently-selected-company: company1;
//here I set current brand but I want to make it load after rest response.
//There are default values for variables below so there won't be any null.
@if ($currently-selected-company == company1) {
$current-header-text-color: $comp1-color;
$logo-right-border-color: $comp1-color;
} @else if ($currently-selected-company == company2) {
$logo-right-border-color: $company2-color;
} @else if ($currently-selected-company == company3) {
$currently-selected-color: $company3-color;
$current-header-text-color: $company3-color;
$selection-color: $company3-color;
$main-color: $company3-color;
$logo-right-border-color: $company3-color;
}
//just few properties to visualize the problem
您获得的错误可能是因为dig
为零,因此在其上调用qDetails.dig("message_stats", "publish")
不起作用
答案 1 :(得分:0)
您可以使用ruby-fetch方法从您不确定密钥是否存在的哈希中获取数据。您还可以将默认值设置为返回,而dig
如果未找到值则返回nil
qDetails["message_stats"].fetch("publish", "default_value")
另一种方法是使用安全导航操作符(&
):
qDetails["message_stats"]&.[]('publish') # Return nil if not found