I am struggling with the error in object and not sure at all where is the problem.
我正在与对象中的错误作斗争,完全不确定问题在哪里。
This is how the models looks like:
模型是这样的:
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
Here is the query:
在这里查询:
@cars = Car.all(:joins => :car_colors, :conditions => { :car_colors => {:color_id => params[:id_number]}}, :order => "cars.created_at DESC")
And the error output:
和错误输出:
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
The generated SQL query (below the error message) seems to be fine, but what causes the error message?
生成的SQL查询(错误消息下面)看起来不错,但是是什么导致了错误消息?
Thank you in advance.
提前谢谢你。
3 个解决方案
#1
33
There likely is a created_at field in your car_colors
table. created_at
should probably be cars.created_at
to remove the ambiguity.
您的car_colors表中可能有一个created_at字段。created_at应该是汽车。created_at消除歧义。
#2
17
Define a scope like this:
定义一个这样的范围:
scope :scope_age, -> { order(created_at: :desc) }
rather than:
而不是:
scope :scope_age, -> { order("created_at DESC") }
It removes the ambiguity by using the property of the model in which the scope is defined in.
它通过使用定义范围的模型的属性来消除模糊性。
#3
1
Don't remove your timestamps from the join model, they aren't the problem - the problem is that something is adding a condition to your query:
不要从连接模型中删除时间戳,它们不是问题所在——问题是有些东西在向查询添加条件:
AND (created_at > '2013-05-03 12:28:36.551058')
Since the date is one month ago, search your code for one.month.ago
and see if it appears in any scopes, probably in your cars or car_colors models. Check the scopes manually if nothing turns up through the search.
因为日期是一个月前,所以搜索你的代码一个月。看看它是否出现在任何范围内,可能出现在您的汽车或car_colors模型中。如果搜索没有出现任何结果,请手动检查范围。
Removing the timestamps will make your query work, but it's not the right thing to do.
删除时间戳将使查询有效,但这不是正确的做法。
#1
33
There likely is a created_at field in your car_colors
table. created_at
should probably be cars.created_at
to remove the ambiguity.
您的car_colors表中可能有一个created_at字段。created_at应该是汽车。created_at消除歧义。
#2
17
Define a scope like this:
定义一个这样的范围:
scope :scope_age, -> { order(created_at: :desc) }
rather than:
而不是:
scope :scope_age, -> { order("created_at DESC") }
It removes the ambiguity by using the property of the model in which the scope is defined in.
它通过使用定义范围的模型的属性来消除模糊性。
#3
1
Don't remove your timestamps from the join model, they aren't the problem - the problem is that something is adding a condition to your query:
不要从连接模型中删除时间戳,它们不是问题所在——问题是有些东西在向查询添加条件:
AND (created_at > '2013-05-03 12:28:36.551058')
Since the date is one month ago, search your code for one.month.ago
and see if it appears in any scopes, probably in your cars or car_colors models. Check the scopes manually if nothing turns up through the search.
因为日期是一个月前,所以搜索你的代码一个月。看看它是否出现在任何范围内,可能出现在您的汽车或car_colors模型中。如果搜索没有出现任何结果,请手动检查范围。
Removing the timestamps will make your query work, but it's not the right thing to do.
删除时间戳将使查询有效,但这不是正确的做法。