在Rails和ActiveRecord中查询时忽略某些字段 [英] Ignoring certain field when querying in Rails and ActiveRecord

查看:76
本文介绍了在Rails和ActiveRecord中查询时忽略某些字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以指定某些字段来使用 pluck 查询数据库。

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)


推荐答案

Model#attribute_names 应该返回列/属性数组。您可以排除其中一些并传递给 pluck select 方法。

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

类似这样的东西:

posts = Post.where(published: true).select(Post.attribute_names - ['enormous_field'])

这篇关于在Rails和ActiveRecord中查询时忽略某些字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆