June 29, 2012

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 Question and 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)
Share on Twitter Share the post
Qihuan Piao

Qihuan Piao

(aka kinopyo) is Chinese based in Tokyo. Software writer. He shares stories inspired him in this blog. His infamous line - "I feel calm when I kill those monsters, or people (in game)" shocks his friends deeply.

He also writes in Japanese and Chinese.