Ignoring certain field when querying in Rails and ActiveRecord

ⅰ亾dé卋堺 提交于 2019-12-23 09:40:36

问题


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)

回答1:


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'])


来源:https://stackoverflow.com/questions/28832738/ignoring-certain-field-when-querying-in-rails-and-activerecord

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!