# Limiting the columns loaded from a legacy db

**URL:** <https://www.ruby-forum.com/t/limiting-the-columns-loaded-from-a-legacy-db/53382>\
**Category:** Rails\
**Created:** [February 3, 2006, 6:03am UTC](https://www.ruby-forum.com/t/limiting-the-columns-loaded-from-a-legacy-db/53382 "2006-02-03T06:03:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blair\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@Blair\_Z](https://www.ruby-forum.com/u/Blair_Z)\
**Post date:** [February 3, 2006, 6:03am UTC](https://www.ruby-forum.com/t/limiting-the-columns-loaded-from-a-legacy-db/53382/1 "2006-02-03T06:03:05Z")

</div>

I’m setting up a new Rails project that is a nice front end for a  
company’s personnel database with a legacy schema.

Unfortunately, the table that contains all the standard information  
(first name, last name, email address) has some 20+ columns, one of  
which is a BLOB of a picture of the person.

Loading all this takes a while, when the application really only  
needs the first and last names and email address.

Is there a way to tell AR not to load all the columns? I looked  
through [api.rubyonrails.com](http://api.rubyonrails.com) and didn’t see anything.

The app will not be making any inserts or updates to the database, so  
I don’t have to be concerned about an AR instance having all the data  
needed to successfully create a new row without violating a database  
constraint (NOT NULL, etc).

Thanks,  
Blair

–  
Blair Z., Ph.D.  
CTO, OrcaWare Technologies  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subversion training, consulting and support  
[http://www.orcaware.com/svn/](http://www.orcaware.com/svn/)

---

<div class="post-metadata">

**Author:** ![Blair\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@Blair\_Z](https://www.ruby-forum.com/u/Blair_Z)\
**Post date:** [February 3, 2006, 6:21am UTC](https://www.ruby-forum.com/t/limiting-the-columns-loaded-from-a-legacy-db/53382/2 "2006-02-03T06:21:10Z")

</div>

Research :select for the find method and if all else fails just use  
find\_by\_sql

Bob S.  
[http://www.railtie.net/](http://www.railtie.net/)

---

<div class="post-metadata">

**Author:** ![Blair\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@Blair\_Z](https://www.ruby-forum.com/u/Blair_Z)\
**Post date:** [February 3, 2006, 6:36am UTC](https://www.ruby-forum.com/t/limiting-the-columns-loaded-from-a-legacy-db/53382/3 "2006-02-03T06:36:18Z")

</div>

Thanks for the tip on :select.

Ideally, you could set something that would apply :select for all  
find methods, including associations, using something like

class Person \< AR  
select\_columns :first\_name, :last\_name, :email\_address  
end

Regards,  
Blair

---

<div class="post-metadata">

**Author:** ![Blair\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@Blair\_Z](https://www.ruby-forum.com/u/Blair_Z)\
**Post date:** [February 3, 2006, 6:48am UTC](https://www.ruby-forum.com/t/limiting-the-columns-loaded-from-a-legacy-db/53382/4 "2006-02-03T06:48:24Z")

</div>

Model.find(:all, :select “normal sql code fragment goes here…  
i.e. first\_name, last\_name, email\_address”)

bruce

---

<div class="post-metadata">

**Author:** ![Blair\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@Blair\_Z](https://www.ruby-forum.com/u/Blair_Z)\
**Post date:** [February 3, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/limiting-the-columns-loaded-from-a-legacy-db/53382/5 "2006-02-03T14:23:32Z")

</div>

Oops!

Model.find(:all, :select=\>"sql code fragment goes here… ie the  
usual select statement you would have used minus the word select)

eg

Model.find(:all, :select=\>“first\_name, last\_name, email\_address”)

bruce

---

<div class="post-metadata">

**Author:** ![Blair\_Z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/76d3ee/32.png) [@Blair\_Z](https://www.ruby-forum.com/u/Blair_Z)\
**Post date:** [February 4, 2006, 6:54pm UTC](https://www.ruby-forum.com/t/limiting-the-columns-loaded-from-a-legacy-db/53382/6 "2006-02-04T18:54:43Z")

</div>

On February 3, 2006 06:22, Bruce B. wrote:

> > > The app will not be making any inserts or updates to the database,  
> > > so I don’t have to be concerned about an AR instance having all  
> > > the data needed to successfully create a new row without violating  
> > > a database constraint (NOT NULL, etc).
> > > 
> > > Thanks,  
> > > Blair

Unfortunately that doesn’t work when you’re also fetching associations  
with :include. Also, unless you explicitly select them in your :select  
snippet, when you join tables into your query their values won’t be  
picked up  
by the AR object (since they’re not being selected). If that’s a  
problem, I  
guess you should override the find method with your own.

Luca
