Wednesday, February 8, 2017

Rails Active Model sorting for text field in psql db

Hello Guys,
     In one scenario of my project, need to show all address with zipcode(stored as text field) sorting. It's good to make sort in db level as this will have better performance when comparing with ruby block.

in ../models/address.rb

def with_zipcode
   Address.where(customer_id: 1).order('cast(zipcode as double precision)')
end


This query will provide all customer address with zipcode in ascending order.


Thanks!!

Cheers up with Coding!!

No comments:

Post a Comment