So after shifting my code to heroku which is in Postgres one of my functions is drawing an error.
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
The particular line that gets highlighted is this
ids = @tutor.merge(tutor).map(&:id)
I have read that certain calls works with sqlite and not with postgres such as doing LIKE ? and such. But i am pretty clueless as to whats wrong here.
Here's the error that is coming up
ActiveRecord::StatementInvalid in TutorsController#index
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. : 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
I don't know what to search to try and resolve this since i dont even know what is it about postgres that is triggering the error.
So hopefully someone can point me in the right direction.
Here's what the tutor model looks like
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
The other subject searches are all the same as self.subject_search.
I think one of the problems i have deduced would be this, in my self.subject_search(s) method, by testing it in the rails console, the line @subject.tutors is drawing the error.
In my rails console i ran subject = Subject.find_by_name("English") followed by subject.tutors and it threw the error
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.
Why though? Im sorry but im really quite bad with postgres and i dont understand whats going on and why it worked with sqlite3. (I read that sqlite3 is not as strict as postgres but that doesn't exactly make it clear for me)
operator does not exist: integer = character varyingYou are comparing (or joining) a varchar field to an integer field.