Forum: Ruby on Rails Limiting the columns loaded from a legacy db

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
27c57aaa4bda5ac8b0593659573b522f?d=identicon&s=25 Blair Zajac (Guest)
on 2006-02-03 06:03
(Received via mailing list)
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 Zajac, Ph.D.
CTO, OrcaWare Technologies
<blair@orcaware.com>
Subversion training, consulting and support
http://www.orcaware.com/svn/
Af93ba6b6b59f22a8f37e8de5702ef98?d=identicon&s=25 Bob Silva (Guest)
on 2006-02-03 06:21
(Received via mailing list)
Research :select for the find method and if all else fails just use
find_by_sql



Bob Silva
http://www.railtie.net/
27c57aaa4bda5ac8b0593659573b522f?d=identicon&s=25 Blair Zajac (Guest)
on 2006-02-03 06:36
(Received via mailing list)
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
2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 Bruce Balmer (brucebalmer)
on 2006-02-03 06:48
(Received via mailing list)
Model.find(:all, :select "normal sql code fragment goes here...
i.e.   first_name, last_name, email_address")

bruce
2dd904ec5981c31e7bb7a5743a53caf8?d=identicon&s=25 Bruce Balmer (brucebalmer)
on 2006-02-03 14:23
(Received via mailing list)
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
67800acb8e18674976b4a90b7463660f?d=identicon&s=25 Luca Pireddu (Guest)
on 2006-02-04 18:54
(Received via mailing list)
On February 3, 2006 06:22, Bruce Balmer 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
This topic is locked and can not be replied to.