限制再psented在ActiveRecord的$ P $列 [英] Restrict the columns represented in ActiveRecord

查看:173
本文介绍了限制再psented在ActiveRecord的$ P $列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何改变的ActiveRecord,使其始终具有有限的一组列。我不想在backened表present模型中的所有列。这不必要地涨大了的ActiveRecord的内存占用量,以及要查询的记录所用的时间。

How to change ActiveRecord so that it always has a restricted set of columns. I dont want all the columns in the backened table to present in the Model. This unnecessarily bloats the ActiveRecord's memory footprint as well as the time taken to query the record.

有喜欢选择属性( ar.rubyonrails.org/classes/ActiveRecord/基的),它可以用于选择仅几列。但有什么办法,我们可以强制的ActiveRecord永远不会查询这些列inspite执行只要找到用户不指定:选择所有时间

There are attributes like select (ar.rubyonrails.org/classes/ActiveRecord/Base) which can be used to SELECT only few columns. But is there any way we can force ActiveRecord to never query those columns inspite of the user performing just find without specifying :select all the time.

推荐答案

使用<一个href="http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-default_scope">default_scope

例如。

class MyModel < ActiveRecord::Base
  default_scope select("column1, column2, column3")

  ...
end

这篇关于限制再psented在ActiveRecord的$ P $列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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