I am trying to set my own method to override a database property, so that I can intercept the value where the database value is null.
Rails seems to allow me to set a method with the same name as a database column, however I would need an alternative way to access the database value to return what I want.
Imagine I have a database column “date”, and I want to set that to something else when it is null in the database.
I would like to set a method as below
def date
if (alternative_db_value_object).date == nil
"Put this"
else
"Put that"
end
end
Alternatively I would greatly appreciate if anyone could suggest a better way of doing this.
.. within the ‘date’ method, you can either use
or
UPDATE: it seems this doesn’t work anymore on rails 5. One is supposed to use
super: