Open ezekg opened 1 week ago
To make matters more complex, you cannot quality the column if the relation is already loaded:
users = User.foos.union(User.bars)
users.load
# raises ActiveRecord::UnmodifiableRelation:
users.maximum('users.updated_at')
# raises ActiveRecord::StatementInvalid:
users.maximum(:updated_at)
# workaround:
users.collect(&:updated_at).max
I'll open up a PR soon.
Not sure if this is a bug in Rails or this gem, but since a subquery name is not provided to
from
here (even though it's technically available on theArel::Nodes::TableAlias
instance), Active Record can't determine its table name. This results in AR using an unqualified column name, which can cause ambiguous column errors for aggregate functions likemaximum(:updated_at)
if there's a join after the union, resulting in anActiveRecord::StatementInvalid
error getting raised.Ideally, AR should be a bit smarter here, since right now it tries to cast the
Arel::Nodes::TableAlias
instance to a string. I'd assume they'd argue Arel is a private API and that this isn't a bug, so I opened the issue here first.Here's a quick script reproducing the
ActiveRecord::StatementInvalid
error:Unfortunately, in addition to one-off aggregations, this also breaks
stale?
andfresh_when
caching/etag controller methods, since they usemaximum(:updated_at)
to calculate the cache's last modified timestamp.The only workaround I could come up with is explicitly qualifying the column name:
Do you think this is worth fixing?