search not working right

when the search gets submitted it only returns results for records that had the searched value in publicists

def self.search(search)
where(“headline LIKE ?”, “%#{search}%”)
where(“storyline LIKE ?”, “%#{search}%”)
where(“publicist LIKE ?”, “%#{search}%”)
end

from my controller:
def index
if params[:search]
@press_releases = PressRelease.search(params[:search]).order(“dateline DESC”)
else
@press_releases = PressRelease.order(“dateline DESC”)
end
end

The where method returns a set of matching records, your method runs
the first where() and throws the result away, then it does the same
with the second and throws the result away, then it runs the third and
returns that. You presumably wish to find records where any of three
are true so you need to use something like
where(("headline LIKE ? OR storyline LIKE ? OR publicist LIKE ?",
"%#{search}%", "%#{search}%", "%#{search}%")

Colin

Thanks Colin, the person who posted that code sure had me fooled, i could have done better myself without adding code to the model It looked like an advanced technique, who would have guessed it was so wrong

Thanks Colin, the person who posted that code sure had me fooled, i could
have done better myself without adding code to the model It looked like an
advanced technique, who would have guessed it was so wrong

Where did you see it posted? Have you still got the link?

Colin

Also in the previous code, you can do ILIKE instead of like to get rid case sensitivity. Just a FYI :slight_smile:

Where did you see it posted? Have you still got the link?

Colin

http://www.korenlc.com/creating-a-simple-search-in-rails-4/

If you read the comments you will see that others have pointed out the
error, though why it has not been corrected is beyond me.

Cheers

Colin