Reputation: 111
I have the following keystring, where column 1 represents the Value, Column 2 is the score, column 3 is the keystring. Column 2 is calculated by checking the column1-value in keystring.
If column-1 value is 7, then it is less than 25. So, Column 2 value will be 0.
To implement this, I made upper_limit and lower_limit.
Lower_Limit | Upper Limit
NULL | 25
25 | 75
75 | 125
125 | 250
250 | 500
500 | NULL
Am using the following Query:
score_line_item = Model.where("upper_limit > ? AND lower_limit <= ?", value, value)
score_line_item = Model.where("upper_limit IS null AND lower_limit >= ? ", value) unless score_line_item.present?
score_line_item = Model.where("lower_limit IS null AND upper_limit > ? ", value) unless score_line_item.present?
How do I implement the same using Rails Way ? (May be in 1 line). I am unable to remove the repetitive lines. I am looking for a better solution for the above question even if it is with Arel table.
Upvotes: 0
Views: 283
Reputation: 6981
Just combine with SQL or
score_line_item = Model.where(
"upper_limit > :value AND lower_limit <= :value OR upper_limit IS null AND lower_limit >= :value OR lower_limit IS null AND upper_limit > :value ",
{value: value}
)
Also does this need to be SQL? Couldn't you just do it in Ruby?
# Usage: credit_scoring(35) #=> 1
def credit_scoring(value)
[25, 75, 125, 250, 500].count do |lower_bound|
lower_bound <= value
end
end
Upvotes: 1