Forum: Ruby on Rails ActiveRecord: Exclude some columns while fetching

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.
zven (Guest)
on 2006-04-23 13:08
(Received via mailing list)
I have models with large TEXT, BLOB columns and I don't wanna fetch them
everytime. Is it possible to omit columns when fetching records from DB?
Sorry, if it's trivial, but I can't find helpful information in docs.
Christer N. (Guest)
on 2006-04-23 13:32
Two alternatives:

1) Define a View, not displaying your blobs.
2) Use find_by_sql. Mention the columns you need.

Christer

zven wrote:
> I have models with large TEXT, BLOB columns and I don't wanna fetch them
> everytime. Is it possible to omit columns when fetching records from DB?
> Sorry, if it's trivial, but I can't find helpful information in docs.
Brian V Hughes (Guest)
on 2006-04-23 19:24
(Received via mailing list)
Third alternative: create a new table that just holds the blob data.
Then make this table a has_one association off the table that you do
all the searching in. Basically, you keep the main table for all the
meta data related to the blobs (which you're always doing finds on)
and only when you need to display the full details of one of those
records do you then build the find with a :include to the has_one table.

It's a little more bookkeeping in your controllers, but it's a
technique I've used several times and it works really well. I also
think it's one of the more Rails-like solutions to this kind of
problem. One thing to keep in mind, if you haven't noticed that your
finds on this model are coming back a little slower than you would
like, using any of these solutions would be "premature optimization",
which goes against normal Agile Developement practices. Basically, if
the database has no trouble giving you the TEXT and BLOB fields on
every find request, why should you care...

-Brian
Bruno C. (Guest)
on 2006-04-23 19:37
(Received via mailing list)
You can use :select option in find method like this:
Model.find :all, :select => "id, name"
Alex W. (Guest)
on 2006-04-23 22:43
Bruno C. wrote:
> You can use :select option in find method like this:
> Model.find :all, :select => "id, name"

This is the right answer, find_by_sql is way overkill for this.
This topic is locked and can not be replied to.