开发者

Lazily load columns in ActiveRecord

开发者 https://www.devze.com 2023-03-31 03:47 出处:网络
So I\'ve been doing some research and have yet to come across a good solution for this. I am trying to avoid loading rarely used columns in an ActiveRecord model.

So I've been doing some research and have yet to come across a good solution for this. I am trying to avoid loading rarely used columns in an ActiveRecord model.

Here's my real world problem: I have an Accounts table:

create_table "accounts", :force => true do |t|
  t.string   "name"
  t.te开发者_开发知识库xt "policies" # this can be a lot of data
end

I pull accounts from the database all the time and I rarely need the policies field. My concern is overhead. Thats extra data I am transferring that I rarely need.

How do I default rails to only pull the name column and grab the policies column when I need it?

I know DataMapper has a solution for this called "lazy load" for attributes. Is there a standard or generally accepted solution for this in ActiveRecord?

Thanks for your help.


The activerecord-lazy-attributes library may provide the functionality you require.

Excerpt from the README:

This ActiveRecord extension allows to define attributes to be lazy-loaded. It’s main purpose is to avoid loading large columns (such as BLOBs) with every SELECT.

0

精彩评论

暂无评论...
验证码 换一张
取 消