I'm new to Rails and I'm using Google Translate to post here. I have a doubt.
I have 2 filters to search: Minimum Qty and Maximum Qty.
However, the user is able to pass Qty min > than Qty Max. Where and how do I validate if the min amount is less than the maximum qty?
scope :filter_total_quantity_min, -> (params) {
params[:total_quantity_min].present? ?
Onde(
"total_quantity >= ?",
params[:total_quantity_min]
)
:
all
}
scope :filter_total_quantity_max, -> (params) {
params[:total_quantity_max].present? ?
Onde(
"total_quantity <= ?",
params[:total_quantity_max]
)
:
all
}
CodePudding user response:
The user enters two values, qty_min and qty_max. If you interpret them literally, then when the user makes a mistake and enters qty_min value greater than qty_max value, then the search will yield no results.
- You can validate using javascript in the browser, before the user submits the form. or
- You can validate in the controller, and return an error if qty_min is greater than qty_max. or
- You can assume that the user has reversed the two values, and re-order them in the controller with
qty_min, qty_max = [qty_min, qty_max].sort
. If you're comfortable with making that assumption.