lostintranslation
lostintranslation

Reputation: 24563

ActiveRecord not filtering when using chained where

  # Get only non-empty reviews
  query = Review.where("content <> ''")

  # filter on status (unreviewed, reviewed, flagged, etc), if provided
  if (status)
    query.where("status = ?", status)
  end

  # Order rows
  query.order("created_on ASC")

  # iterate, this should fire the query
  reviews = query.each_with_object([]) do |review, a|

My problem is that my query is not filtering on 'status'. I have doubled checked that my code is getting in the if statement. I have even gone as far as:

  # Get only non-empty reviews
  query = Review.where("content <> ''")

  # filter on status (unreviewed, reviewed, flagged, etc), if provided
  query.where("status = ?", status)

I am sure that I am doing something wrong but I have no idea

Upvotes: 0

Views: 30

Answers (1)

lostintranslation
lostintranslation

Reputation: 24563

Just found it. Need to assign the query each time

  # Get only non-empty reviews
  query = Review.where("content <> ''")

  # filter on status (unreviewed, reviewed, flagged, etc), if provided
  query.where("status = ?", status)

Upvotes: 1

Related Questions