I have a model sensor
that stores environmental data from an Arduino(temperature, humidity).
create_table "sensors", force: :cascade do |t|
t.integer "system_id"
t.float "value"
t.datetime "created_at"
end
I want to get the average value by all my devices. To do it I need to get the last values from all devices by system_id
.
The result I would like to get should look like this:
[#<Sensor:0x0000556dd6a5c758
id: 10,
sensor_id: 1,
value: 14.0,
created_at: Sat, 31 Jul 2021 18:41:13 UTC +00:00,
#<Sensor:0x0000556dd6a6f448
id: 149,
sensor_id: 2,
value: 22.0,
created_at: Sat, 31 Jul 2021 18:41:13 UTC +00:00,
#<Sensor:0x0000556dd6a6f238
id: 329,
sensor_id: 3,
value: 11.0,
created_at: Sat, 31 Jul 2021 18:41:13 UTC +00:00,
...]
or just array of values corresponding to the task conditions:
[14.0, 22.0, 11.0]
I try it like this:
Sensor.order('created_at DESC').distinct(:system_id)
But it doesn't work. Any idea how to do this?