Can anyone help me with this problem?
So, here is the problem, I want to merge this query response:
@energy = Alert.where(["alert_type = ?", "Energy"]).last.as_json
@cost = Alert.where(["alert_type = ?", "Cost"]).last.as_json
Then I merge those object with:
@current_notif = @energy.merge(@cost)
But those just give me @cost object like this:
{
"alert_type": "Cost",
"value": 30000000,
"status": "Cost exceeds limit",
"created_at": "2017-06-03T15:31:21.156+07:00",
"updated_at": "2017-06-03T15:31:21.156+07:00",
"home_id": 2
}
Rather than a merged @energy + @cost like this:
{ {"alert_type": "Energy",
"value": 384455.813978742,
"status": "Energy too high",
"created_at": "2017-05-31T11:31:12.907+07:00",
"updated_at": "2017-05-31T11:31:12.907+07:00",
"home_id": 2 },
{
"alert_type": "Cost",
"value": 30000000,
"status": "Cost exceeds limit",
"created_at": "2017-06-03T15:31:21.156+07:00",
"updated_at": "2017-06-03T15:31:21.156+07:00",
"home_id": 2
}
}
.mergeis behaving the way it is supposed to. Read the docs: docs.ruby-lang.org/en/2.0.0/Hash.html#method-i-merge. Also, that resulting hash isn't a valid hash. Are you sure you don't want anarray?