I'm new here and new to rails. I'm currently trying to make an own search function for the database (books) of my training project. I don't want to implement an existing search form because I just want to learn. So far it is working but now I added a dropdown menu to choose a comparison symbol from. Based on that it shall find the books with a rating ("==", ">=" or "<=").
I tried setting the if clauses before the whole where-part but that means I would need to have the whole where-part three times. I was hoping there is a shorter way to achieve that? Thank you in advance!
def self.advanced_search(s_name, s_author, s_comp_sign, s_rating)
where("lower(name) LIKE ?", "%#{s_name}%").
where("lower(author) LIKE ?", "%#{s_author}%").
where(:rating == s_rating) #if s_comp_sign == "="
where(:rating >= s_rating) #if s_comp_sign == ">"
where(:rating <= s_rating) #if s_comp_sign == "<"
end
Aucun commentaire:
Enregistrer un commentaire