Home > OS >  Scope validation of min quantity informed less than the maximum
Scope validation of min quantity informed less than the maximum

Time:08-13

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.

  1. You can validate using javascript in the browser, before the user submits the form. or
  2. You can validate in the controller, and return an error if qty_min is greater than qty_max. or
  3. 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.
  • Related