I have entries table with a content field which might contain a lot of text. In most cases I don’t need to access that field, so it seems to be a big waste of resources to every time load a huge amount of unused data from the database (select * from entries where id = 1).
How could I specify the default_scope, that all the fields apart from content would be loaded from database?
Assuming Rails 3 and a schema that looks like this:
You can use the
selectmethod to limit the fields that are returned like this:In the rails console, you should see something like this:
When you do want to select all of the fields, you can use the
unscopedmethod like this: