0
我試圖通過EventViews到列表中的用戶,並責令其選擇,但我不斷收到一個錯誤:的has_many通過與秩序不工作
Unknown column 'views_count' in 'order clause': SELECT `users`.`id` AS t0_r0, `users`.`email` AS t0_r1, `users`.`encrypted_password` AS t0_r2, `users`.`reset_password_token` AS t0_r3, `users`.`reset_password_sent_at` AS t0_r4, `users`.`remember_created_at` AS t0_r5, `users`.`sign_in_count` AS t0_r6, `users`.`current_sign_in_at` AS t0_r7, `users`.`last_sign_in_at` AS t0_r8, `users`.`current_sign_in_ip` AS t0_r9, `users`.`last_sign_in_ip` AS t0_r10, `users`.`created_at` AS t0_r11, `users`.`updated_at` AS t0_r12, `users`.`user_type` AS t0_r13, `event_views`.`id` AS t1_r0, `event_views`.`event_id` AS t1_r1, `event_views`.`ip` AS t1_r2, `event_views`.`created_at` AS t1_r3, `event_views`.`updated_at` AS t1_r4, `event_views`.`session_id` AS t1_r5, `event_views`.`is_unique` AS t1_r6, `event_views`.`user_agent` AS t1_r7, `event_views`.`promoter_id` AS t1_r8 FROM `users` LEFT OUTER JOIN `events` ON `events`.`user_id` = `users`.`id` LEFT OUTER JOIN `event_views` ON `event_views`.`event_id` = `events`.`id` ORDER BY views_count desc
這裏是我的代碼:
User.all.includes(:event_views).select('user.*, count(event_views.id) as "views_count"').order("views_count desc").each
#models
User
has_many :event_views, :through => :events
Events
has_many :event_views
EventView
#nothing relevant
他們是所有相關的正常時尚(event_id,user_id等)
不,我要計算event_views,而不是事件 – Rob
您是否在您的問題中正確複製了查詢?你有'用戶。*'而不是'用戶。*'。另外,我希望你得到一個錯誤,指出缺少一個'group by'子句。 –