rcrcr
rcrcr

Reputation: 65

PostgreSQL syntax error Rails 4

I have a Rails 4 app deployed on heroku.

In development using MySQL I receive no errors.

In production on heroku I get the following:

Mar 16 09:40:22 squareshaped app/web.1:  Started GET "/" for 87.83.45.218 at 2015-03-16 16:40:22 +0000 
Mar 16 09:40:22 squareshaped app/web.1:  Processing by PagesController#index as HTML 
Mar 16 09:40:23 squareshaped app/web.1:  PG::SyntaxError: ERROR:  syntax error at or near ")" 
Mar 16 09:40:23 squareshaped app/web.1:  LINE 1: SELECT COUNT(*) FROM "courses"  WHERE "courses"."id" IN () 
Mar 16 09:40:23 squareshaped app/web.1:                                                                   ^ 
Mar 16 09:40:23 squareshaped app/web.1:  : SELECT COUNT(*) FROM "courses"  WHERE "courses"."id" IN () 
Mar 16 09:40:23 squareshaped app/web.1:    Rendered pages/index.html.erb within layouts/application (69.8ms) 
Mar 16 09:40:23 squareshaped app/web.1:  ActionView::Template::Error (PG::SyntaxError: ERROR:  syntax error at or near ")" 
Mar 16 09:40:23 squareshaped app/web.1:  LINE 1: SELECT COUNT(*) FROM "courses"  WHERE "courses"."id" IN () 
Mar 16 09:40:23 squareshaped app/web.1:                                                                   ^ 

I thought that this might be because my code is asking it to look up a value in an empty array. So I added an if statement so it wouldn't look up if the array was empty. This isn't working.

def index
@courses = Course.limit(7).order(created_at: :desc)
@reviews = Review.limit(7).order(created_at: :desc)
if user_signed_in?
  # Find peer recommended courses
  @peers = User.where("position LIKE ?", current_user.position).where('id <> ?', current_user.id)
  @peer_recommended_courses = []
  @peers.each do |peer|
    @peer_recommended_courses << peer.reviews.where(recommended: true).pluck(:course_id)
  end
  if @peer_recommended_courses.empty?
    @peer_recommendations = []
  else
    @peer_recommendations = Course.where(id: @peer_recommended_courses)
  end

  #Find company recommended courses
  @colleagues = User.where("company LIKE ?", current_user.company).where('id <> ?', current_user.id)
  @colleague_recommended_courses = []
  @colleagues.each do |peer|
    @colleague_recommended_courses << peer.reviews.where(recommended: true).pluck(:course_id)
  end
  if @colleague_recommended_courses.empty?
    @colleague_recommendations = []
  else
    @colleague_recommendations = Course.where(id: @colleague_recommended_courses)
  end

end
end

I'd be grateful if anyone could explain why this error is occurring.

p.s. apologies for awful coding, I'm very much a noob and think that most of this should probably be in my Course model. I'm working on it. Just want to ship this update.

Upvotes: 0

Views: 620

Answers (1)

Lukas Eklund
Lukas Eklund

Reputation: 6138

The query is still running because @peer_recommended_courses isn't an empty array.

@peer_recommended_courses = []
  @peers.each do |peer|
    @peer_recommended_courses << peer.reviews.where(recommended: true).pluck(:course_id)
end

If if there are no peer reviews where recommended is true the value of @peer_recommended_courses is now actually [[]] not just []. Flatten the array so that Arel handles the query properly (or flatten the array before the empty? check):

@peer_recommendations = Course.where(id: @peer_recommended_courses.flatten)

Upvotes: 2

Related Questions