Limiting the columns loaded from a legacy db


#1

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 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
Subversion training, consulting and support
http://www.orcaware.com/svn/


#2

Research :select for the find method and if all else fails just use
find_by_sql

Bob S.
http://www.railtie.net/


#3

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


#4

Model.find(:all, :select “normal sql code fragment goes here…
i.e. first_name, last_name, email_address”)

bruce


#5

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


#6

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