I need to create a search query that finds all users with specified hourly rates. The users have hourly rate ranges such as $55/h to $75/h. A site visitor will search for these users with another range such as $40/h to $60, which should display all overlapping registered users.
The old working plain string query was:
(users.hour_price_low BETWEEN :low_price AND :high_price OR hour_price_high BETWEEN :low_price AND :high_price).
I need to refactor this to searchable in an array query. My starting point for the refactor was this RailsCast.
I have modified the methods to accomodate OR clauses, no problems there.
##This is where I am getting hung up. I can't get the syntax correct.##
def low_price_or_conditions
["users.hour_price_low BETWEEN ?", price_low..price_high] unless price.blank?
end
def high_price_or_conditions
["users.hour_price_high BETWEEN ?", price_low..price_high] unless price.blank?
end
Finally:
@users = User.where(conditions)
Which outputs this error:
Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80 OR users.hour_pr' at line 1: SELECT `users`.* FROM `users` WHERE (users.hour_price_low BETWEEN 60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80 OR users.hour_price_high BETWEEN 60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80)
I am open to any suggestions. Thanks!
Skip the range, and just use the between clause as you were previously.
ie