4 ms·
This is something you should be able to do with Arel (with join_sources) or through the use of to_sql as a note - you shouldn't use includes and joins, include
by omarqureshi 14y ago
This is something you should be able to do with Arel (with join_sources) or through the use of to_sql
as a note - you shouldn't use includes and joins, includes hammers joins.
Simplest tidy up though is a scope for the join, something like
DeviceConfig.joins("#{DeviceConfig.last_updated.to_sql} dc2
on dc2.device_id = device_configs.device_id
and dc2.max_updated_at = device_configs.updated_at).
order(...)
Alternatively maybe something like
dc_t = Arel::Table::new :device_configs
dc2_t = Arel::Table::new :device_configs
inner = dc_t.group.(dc_t[:device_id]).
project(dc_t[:device_id].as("device_id"),
dc_t[:updated_at].maximum.as("maximum_updated_at"))
dc2_t.join(inner.join_sources).
on(dc2_t[:device_id].eq(inner[:device_id])).
on(dc2_t[:updated_at].eq(inner[:max_updated_at])).
order(...)
im not entirely sure of the on syntax there, the API doesnt look clear.
Atleast, those are the two solutions I have though personally - i think it reads better as straight SQL with select_values. That way, you know what is happening.