Count of a relation using arel in active record
I'm having a really rough time figuring out how to do this query and others like it in arel from active record.
select users.id,
users.name,
maps.count as map_count,
from us开发者_如何学Goers
left join (select user_id, count(map_id) as count from maps_users group by user_id) maps on users.id = maps.user_id
On the surface, it looks just like Nik's example here (http://magicscalingsprinkles.wordpress.com/2010/01/28/why-i-wrote-arel/):
photo_counts = photos.
group(photos[:user_id]).
project(photos[:user_id], photos[:id].count)
users.join(photo_counts).on(users[:id].eq(photo_counts[:user_id]))
But I can't get it to work in rails using active record. I think the equivalent should be something like this, but it errors out :(
maps = Map.arel_table
map_counts = Map.group(maps[:owner_id]).
select(maps[:owner_id]).
select(maps[:id].count.as("map_count"))
users = User.joins(map_counts).on(User.arel_table[:id].eq(map_counts[:map_count]))
Any ideas on how to do it?
Well first replace the select with project. In relational algebra SELECT (restriction) is the WHERE clause.
Secondly you can do subselections.
sub_restriction = b.
where( b[:domain].eq(1) ).
project( b[:domain] )
restriction = a.
where( a[:domain].in sub_restriction )
"sub selections" DONE! :-)
Yeah, that article really made me want to learn Arel magic, too.
All the "do something intelligent with Arel" questions on Stackoverflow are getting answered with SQL. From articles and research, then, I can say that Arel is Not ActiveRecord. Despite the dynamic formulation of queries, Active doesn't have the power to map the results of a fully formed Arel projection.
You get the ability to specify operators with
https://github.com/activerecord-hackery/squeel
but no subselects.
Updated: OMG, I answered this question 5 years ago. No kidding the link was dead :)
精彩评论