简体   繁体   English

修补ActiveRecord / postgres_ext以返回大写的UUID

[英]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. 我将Postgres_ext与Rails / ActiveRecord结合使用,以便能够将UUID存储为PostreSQL UUID数据类型。 But in my app I'm comparing uuids and would like the UUIDs returned from the models to be in uppercase instead of lowercase. 但是在我的应用程序中,我正在比较uuid,并希望从模型返回的UUID为大写而不是小写。

How do I patch ActiveRecord/postgres_ext to always return uppercase string representations of the UUIDs? 如何修补ActiveRecord / postgres_ext以始终返回UUID的大写字符串表示形式?

I have a question as to why you want to do this, but... 关于您为什么要这样做,我有一个疑问,但是...

Patching ActiveRecord may not be the best way to do this. 修补ActiveRecord可能不是执行此操作的最佳方法。 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. 如果您希望模型将UUID的大写版本返回给应用程序的其余部分,则可以在模型代码本身中执行此操作。 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Rails 3.2模式转储使用postgres_ext gem将所有UUID列转换为text和mangles数组声明 - Rails 3.2 schema dump turns all UUID columns to text and mangles array declarations with postgres_ext gem 在postgis适配器中使用postgres_ext不适用于数组列 - Using postgres_ext with postgis adapter not working with array columns postgres_ext / serializers help ...看起来很简单,但无法按照描述获得任何工作 - postgres_ext/serializers help… seems so simple, but can't get anything to work as described 计算Active Admin仪表板(Rails,Active admin 1.0,Postgresql数据库,postgres_ext gem)中序列化属性(数组)中值的出现 - Count occurrence of values in a serialized attribute(array) in Active Admin dashboard (Rails, Active admin 1.0, Postgresql database, postgres_ext gem) 尝试在Active Admin table_for(Rails,Postgresql,postgres_ext gem)上使用数组数据时,[xxx]的错误未定义方法`to_key':Array: - Error undefined method `to_key' for [xxx] :Array when trying to use array data on Active Admin table_for (Rails, Postgresql, postgres_ext gem) Rails + ActiveRecord + Postgres:如何匹配不区分大小写的子字符串? - Rails + ActiveRecord + Postgres: How to match case insensitive substring? Rails activeRecord补丁和提交 - Rails activeRecord patch and commit 什么是postgres中的uuid ossp - What is uuid ossp in postgres 如何查询 postgres 的 UUID - How to query UUID for postgres 如何在最小的测试案例中使ActiveRecord返回时间戳为TimeWithZone? - How to make ActiveRecord return timestamp as TimeWithZone in a minimal test case?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM