ruby-on-rails – Rails ActiveRecord:PG ::错误:错误:列引用“created_at”是不明确的

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails ActiveRecord:PG ::错误:错误:列引用“created_at”是不明确的前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在努力处理对象中的错误,并且不确定问题在哪里.

这就是模型的样子:

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来消除歧义.
原文链接:/ruby/273128.html

猜你在找的Ruby相关文章