# Delaying table attribute load using ActiveRecord

**URL:** <https://www.ruby-forum.com/t/delaying-table-attribute-load-using-activerecord/93418>\
**Category:** Rails\
**Created:** [April 2, 2006, 12:19pm UTC](https://www.ruby-forum.com/t/delaying-table-attribute-load-using-activerecord/93418 "2006-04-02T12:19:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![llecigne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ccd318/32.png) [@llecigne](https://www.ruby-forum.com/u/llecigne)\
**Post date:** [April 2, 2006, 12:19pm UTC](https://www.ruby-forum.com/t/delaying-table-attribute-load-using-activerecord/93418/1 "2006-04-02T12:19:17Z")

</div>

Hi all,

I came accross the following problem lately: I got a table with several  
BLOB fields (Oracle DB) and I have ActiveRecord reading those fields  
whenever records are loaded using finder methods. This behavior is just  
fine in 99.9% of the time, but considering BLOB data is not small, it is  
considerably slowing down search access :(.

So basically, I thought about building a :has\_one relationship and add  
an extra table table holding BLOB value so as to cope with the pb. I  
found it painfull and not very ‘RoRy’, so I decided to hack  
ActiveRecord::Base so as to delay the attribute load… and it works.  
Attributes are dynamicaly loaded whenever client application accesses  
them explicitely.

Syntax looks like the following:

class A \< ActiveRecord::Base  
delay\_attributes :foo, :bar  
…  
end

Now I got two questions:

- as I am new Rails (and Ruby as well): I am wondering if I am missing  
something concerning ActiveRecord usage (meaning this hack is useless),
- if this patch is a good idea, I’d like to submit it to the  
maintainers of the project, but I don’t know hat is the exact procedure  
to do so.

Thx

---

<div class="post-metadata">

**Author:** ![llecigne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ccd318/32.png) [@llecigne](https://www.ruby-forum.com/u/llecigne)\
**Post date:** [April 3, 2006, 4:11pm UTC](https://www.ruby-forum.com/t/delaying-table-attribute-load-using-activerecord/93418/2 "2006-04-03T16:11:05Z")

</div>

On 4/2/06, Laurent L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> found it painfull and not very ‘RoRy’, so I decided to hack
> 
> Now I got two questions:
> 
> - as I am new Rails (and Ruby as well): I am wondering if I am missing  
> something concerning ActiveRecord usage (meaning this hack is useless),
> - if this patch is a good idea, I’d like to submit it to the  
> maintainers of the project, but I don’t know hat is the exact procedure  
> to do so.
> 
> Thx

You can use the new scoped finders with the :select option to prevent  
ActiveRecord from pulling in the BLOB column. You could then make a  
separate accessor method to grab the BLOB when you need it.

---

<div class="post-metadata">

**Author:** ![llecigne](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ccd318/32.png) [@llecigne](https://www.ruby-forum.com/u/llecigne)\
**Post date:** [April 4, 2006, 8:59am UTC](https://www.ruby-forum.com/t/delaying-table-attribute-load-using-activerecord/93418/3 "2006-04-04T08:59:16Z")

</div>

Wilson B. wrote:

> On 4/2/06, Laurent L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > found it painfull and not very ‘RoRy’, so I decided to hack
> > 
> > Now I got two questions:
> > 
> > - as I am new Rails (and Ruby as well): I am wondering if I am missing  
> > something concerning ActiveRecord usage (meaning this hack is useless),
> > - if this patch is a good idea, I’d like to submit it to the  
> > maintainers of the project, but I don’t know hat is the exact procedure  
> > to do so.
> > 
> > Thx
> 
> You can use the new scoped finders with the :select option to prevent  
> ActiveRecord from pulling in the BLOB column. You could then make a  
> separate accessor method to grab the BLOB when you need it.

Thanks for your quick and accurate answer 🙂

I guess you are right, scoped finders can do the job pretty well.  
However, I have still to specify the scope in every calls to the  
repository (could be error prone) and take care of some accessor (even  
if it is no big deal though). While scoped finders mainly dynamically  
adapt queries given the runtime context (for security purpose mainly),  
delaying some table attribute load is rather a static behavior, non  
context oriented and a good workaround for coping with large columns  
slow access.  
As my application is not very big now, I’ll try to use both approaches  
to see whether it is still a good idea to have ActiveRecord::Base  
patched or not (which sucks a lot cause I have to scratch my head every  
time activerecord is released!).
