【发布时间】:2017-03-08 14:13:06
【问题描述】:
所以在将我的代码转移到 Postgres 中的 heroku 之后,我的一个函数出现了错误。
def index
@tutor = Tutor.where(:admin => false)
@tutor_array = []
@tutor_array << @tutor.fees_search(params[:fees_search]) if params[:fees_search].present?
@tutor_array << @tutor.subject_search(params[:subject_search]) if params[:subject_search].present?
@tutor_array << @tutor.lssubject_search(params[:lssubject_search]) if params[:lssubject_search].present?
@tutor_array << @tutor.ussubject_search(params[:ussubject_search]) if params[:ussubject_search].present?
@tutor_array << @tutor.jcsubject_search(params[:jcsubject_search]) if params[:jcsubject_search].present?
@tutor_array.each do |tutor|
ids = @tutor.merge(tutor).map(&:id)
@tutor = Tutor.where(id: ids)
end
@tutor = @tutor.sort_by { |tutor| tutor.rating.rating }.reverse
@tutor = @tutor.paginate(:page => params[:page], :per_page => 2)
end
突出显示的特定行是
ids = @tutor.merge(tutor).map(&:id)
我已经读到某些调用适用于 sqlite 而不是 postgres,例如 LIKE ? 等。但我对这里出了什么问题一无所知。
这是即将出现的错误
TutorsController#index 中的 ActiveRecord::StatementInvalid
PG::UndefinedFunction: 错误: 运算符不存在: 整数 = 字符变化 LINE 1: ...M "tutors" INNER JOIN "profiles" ON "tutors"."id" = "profile... ^ 提示:没有运算符与给定名称匹配 和参数类型。您可能需要添加显式类型转换。 : SELECT "tutors".* FROM "tutors" INNER JOIN "profiles" ON "tutors"."id" = "profiles"."tutor_id" INNER JOIN "profile_ussubjects" ON "profiles"."id" = "profile_ussubjects"."profile_id" WHERE "tutors"."admin" = $1 AND "profile_ussubjects"."ussubject_id" = $2
我不知道要搜索什么来尝试解决这个问题,因为我什至不知道触发错误的 postgres 是什么。
所以希望有人能指出我正确的方向。
这是导师模型的样子
def self.fees_search(n)
@profile = Profile.fees(n)
if @profile.empty?
return Tutor.none
else
@profile.map do |y|
y.tutor
end
end
end
def self.subject_search(s)
@subject = Subject.find_by_name(s)
unless @subject.nil?
@subject.tutors
end
end
其他主题搜索与self.subject_search相同。
我认为我推断出的问题之一是,在我的self.subject_search(s) 方法中,通过在rails 控制台中对其进行测试,@subject.tutors 行正在绘制错误。
在我的 Rails 控制台中,我运行subject = Subject.find_by_name("English"),然后运行subject.tutors,它抛出了错误
ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR: operator does not exist: integer = character varying
LINE 1: ...M "tutors" INNER JOIN "profiles" ON "tutors"."id" = "profile...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
为什么?对不起,但我对 postgres 真的很不好,我不明白发生了什么以及为什么它与 sqlite3 一起工作。 (我读到 sqlite3 不像 postgres 那样严格,但这对我来说并不完全清楚)
【问题讨论】:
-
operator does not exist: integer = character varying您正在将 varchar 字段与整数字段进行比较(或连接)。 -
所以如果我不能做@tutor.merge(tutor) 这似乎是什么触发了错误(合并)有没有办法解决它?还是我必须以完全不同的方式重组我的控制器代码?
-
您当前的代码在性能方面可能很糟糕 - 您应该构建一个范围,而不是单独获取一堆 id 并将它们添加在一起。您能否将 Tutor 模型添加到问题中,以便我们查看所有这些搜索方法的作用?
-
我已经添加了它。老实说,我实际上不再需要它以完全相同的方式运行,因为我不再需要能够同时通过所有不同的主题搜索。我更关心出现错误的原因