I'm trying to clean up an ActiveRecord code written with MySQL case statements using ARel. I know Arel is able to handle case statements, but not sure how to use it.
So I have an orders table with quantity and user_id(foreign key) columns. I'm dynamically computing the price of orders as there are different pricing schemes based on the quantity ordered. This is what the AR code looks like:
def orders_with_price
<<-SQL
orders.*,
SUM(CASE orders.quantity
WHEN 2 THEN 500 * orders.quantity
WHEN 5 THEN 450 * orders.quantity
WHEN 10 THEN 350 * orders.quantity
END) AS total_price
SQL
end
Order.
select(orders_with_price).
group("orders.user_id").
having("total_price > ?", minimum_price).
order("total_price")