[英]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.