Explicitly specify table name in scope definition
I was wondering why people explicitly specify table name in such a scope definition...
class Answer < ActiveRecord::Base scope :recent, order('answers.created_at DESC') end
Until I got this error today.
Mysql2::Error: Column 'created_at' in order clause is ambiguous: SELECT COUNT(DISTINCT `answers`.`id`) AS count_id, question_id AS question_id FROM `answers` LEFT OUTER JOIN `questions` ON `questions`.`id` = `answers`.`question_id` WHERE (`answers`.user_id = 87) GROUP BY question_id ORDER BY created_at DESC LIMIT 10
I have a
recent scope defined in both
Answer model. And when you chain those two models, rails don't know the
recent is associated to which one.
@answers = @user.answers.recent.group(:question_id).includes(:question).limit(10)