ActiveRecord: Exclude some columns while fetching

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.

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.

You can use :select option in find method like this:
Model.find :all, :select => “id, name”

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.

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