How to add a search feature to my code?

self-taught

#1

Hi Everyone,

I’ve been working on adding a simple search feature to my code for about 10 hours now and I have no idea what I’m doing wrong. :frowning: I have a project I am making where I have a main page of cars and I can click on each car and click on a page to get a listing of multiple models of the car that are stored in the database.

The problem is when I search on the “listing” page I get an error “ActiveRecord::StatementInvalid in Listings#index” and I have no idea how to resolve it.

I’ll venmo $10 to the first person to help me solve this. I’m desperate! Thank you. Code is public and can be found here: https://github.com/erika-kuntar/SomethingCarRelated


#2

I’ll see if I can clone your project and take a look


#3

I just realized that I left my repo on a db:migrate error when you clone it. I just fixed it, so sorry about that.

I saw you mentioned scoping on the chat, I was actually looking into scoping but the tutorial I was following made it seem kind of static. I will look up more tutorials!!


#4

Hi @erikakuntar

The issue is in the search_by function for listing

  def self.search_by(search_term)
    where("LOWER(name) LIKE :search_term", search_term: "%#{search_term.downcase}%")
  end

I assume you just copied this from the products search_by function. The issue is that while products does have a name column the listing column does not so LOWER(name) fails and throws an error. I changed name to model which your listings table does have and it seems to work ok. You just need to decide if model is what you want to search on.

Hope that helps.


#5

Also I only actually reread your original message now properly as I already saw you mention this issue in the chatrooms.

I didn’t solve this for the $10. Keep the money and just pay forward the help when you have the answer somebody else needs or you can buy $10 of food and donate to your local foodbank if you can spare the money.


#6

OMG WOWWW. You are a genius. Amazingggg! I cannot believe that little word got me so hung up. Oh man. Hours of looking at this code. Wowowowow. I cannot tell you thank you enough. I will do both of those things, food bank and pay it forward for others. You’re truly a remarkable and inspiring person.


#7

Also thank you for taking the time to explain in detail the error of what I was doing. That’s so huge, now I know what to look out for in the future. Seriously! THANK YOU!


#8

No problem.

The error message being thrown, although a little cryptic, did say there was no column name for the listings table so I checked the schema file in the db folder to check what columns each table had.

Good luck with the rest of the project.