我正在努力处理对象中的错误,并且不确定问题在哪里.
这就是模型的样子:
class Car < ActiveRecord::Base has_many :car_colors has_many :colors,:through => :car_colors end class CarColor < ActiveRecord::Base belongs_to :color belongs_to :car end class Color < ActiveRecord::Base has_many :car_colors has_many :cars,:through => :car_colors end
这是查询:
@cars = Car.all(:joins => :car_colors,:conditions => { :car_colors => {:color_id => params[:id_number]}},:order => "cars.created_at DESC")
PG::Error: ERROR: column reference "created_at" is ambiguous LINE 1: ...d" WHERE "car_colors"."color_id" = 2 AND (created_at... ^ : SELECT "cars".* FROM "cars" INNER JOIN "car_colors" ON "car_colors"."car_id" = "cars"."id" WHERE "car_colors"."color_id" = 2 AND (created_at > '2013-05-03 12:28:36.551058') ORDER BY cars.created_at DESC
生成的SQL查询(错误消息下面)似乎很好,但是什么原因导致错误消息?
先谢谢你.
解决方法
car_colors表中可能有一个created_at字段. created_at应该是cars.created_at来消除歧义.