停止 Activerecord 加载 Blob 列 [英] Stop Activerecord from loading Blob column

查看:17
本文介绍了停止 Activerecord 加载 Blob 列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除非明确要求,否则如何告诉 Activerecord 不加载 blob 列?我的旧数据库中有一些非常大的 blob,必须将其排除在正常"对象之外.

How can I tell Activerecord to not load blob columns unless explicitly asked for? There are some pretty large blobs in my legacy DB that must be excluded for 'normal' Objects.

推荐答案

我刚刚在使用 rail 3 时遇到了这个问题.

I just ran into this using rail 3.

幸运的是,这并不难解决.我设置了一个 default_scope,它从结果中删除了我不想要的特定列.例如,在我的模型中,有一个 xml 文本字段可能很长,但在大多数视图中都没有使用.

Fortunately it wasn't that difficult to solve. I set a default_scope that removed the particular columns I didn't want from the result. For example, in the model I had there was an xml text field that could be quite long that wasn't used in most views.

default_scope select((column_names - ['data']).map { |column_name| "`#{table_name}`.`#{column_name}`"})

您将从解决方案中看到,我必须将列映射到完全限定的版本,以便我可以通过关系继续使用模型,而不会在属性中产生歧义.稍后您确实想让该字段添加另一个 .select(:data) 以将其包含在内.

You'll see from the solution that I had to map the columns to fully qualified versions so I could continue to use the model through relationships without ambiguities in attributes. Later where you do want to have the field just tack on another .select(:data) to have it included.

这篇关于停止 Activerecord 加载 Blob 列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆