I have a table in postgres with date_of_birth and would like to use the Postgres age function to return the age, rather than calculate the age in Rails (this is a simple example, I will want to do more complicated calculations in postgres).
What is the best way to get the age back from postgres with the rest of my record, ideally as standard without having to modify every select?
EDIT:
I've decided to use views because:
people = Person.select('*, age(date_of_birth)')
people.each { |person| puts person.age }
Yes, this will add a method, age
, that did not previously exist on Person
You can also alias the new method to something other than the function name:
people = Person.select('*, age(date_of_birth) as foo')
people.each { |person| puts person.foo }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With