Search code examples
ruby-on-railsrubypostgresqlactiverecordpostgres-ext

Patch ActiveRecord/postgres_ext to return upper case UUID


I'm using postgres_ext with Rails/ActiveRecord to be able to store UUID as the PostreSQL UUID datatype. But in my app I'm comparing uuids and would like the UUIDs returned from the models to be in uppercase instead of lowercase.

How do I patch ActiveRecord/postgres_ext to always return uppercase string representations of the UUIDs?


Solution

  • I have a question as to why you want to do this, but...

    Patching ActiveRecord may not be the best way to do this. If you want your models to return the uppercase version of the UUID to the rest of your application, you can do this in the model code itself. That way, you're abstracting the details of the database layer away from the rest of the application.

    You can do this by overriding the accessor code. Might look like:

    def uuid
      read_attribute(:uuid).upcase
    end