我有一个非常艰难的时间来确定如何执行此查询以及其他类似于活动记录中的arel.
select users.id,users.name,maps.count as map_count,from users left join (select user_id,count(map_id) as count from maps_users group by user_id) maps on users.id = maps.user_id@H_502_4@从表面上看,它看起来就像Nik的例子(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]))@H_502_4@但是我不能让它在使用活动记录的rails中工作.我认为等效应该是这样的,但它出错了:(
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]))@H_502_4@关于如何做的任何想法?