ruby on rails - Trying to check a String number against Range -


i have situation data number, , saved string. have range want check string against. item in range integer. how can this? below code scope writing:

if value == "above"   scope.where(["level > ?", level_range.last]) elsif value == "below"   scope.where(["level < ?", level_range.first]) elsif value == "at"   scope.where(:level => level_range) else   scope end 

you level numbers stored strings. first of all, that's kinda dumb; if can, fix schema store numeric. if can't change schema, , db ansi compliant, can use cast() on stored value perform integer comparisons range parameters.

if value == "above"   scope.where(["cast(level integer) > ?", level_range.last]) elsif value == "below"   scope.where(["cast(level integer) < ?", level_range.first]) elsif value == "at"   scope.where(["cast(level integer) between ? , ?",               level_range.first, level_range.last]) else   scope end 

one caveat behaviour of cast if string not valid integer representation throw error; reason type data correctly in schema.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -