Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring certain field when querying in Rails and ActiveRecord

I know that I can specify certain fields to query the database with pluck.

ids = Item.where('due_at < ?', 2.days.from_now).pluck(:id)

However I would like to know, whether there is a way to specify certain fields I would like to avoid querying from the db. Some kind of anti-pluck?

  posts = Post.where(published: true).do_not_lookup(:enormous_field)
like image 293
The Whiz of Oz Avatar asked Mar 03 '15 13:03

The Whiz of Oz


1 Answers

Model#attribute_names should return array of columns/attributes. You can exclude some of them and pass to pluck or select methods.

Something like this:

posts = Post.where(published: true).select(Post.attribute_names - ['enormous_field'])
like image 57
Maxim Avatar answered Oct 26 '22 05:10

Maxim