加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 在Rails和ActiveRecord中查询时忽略某个字段

发布时间:2020-12-17 02:48:06 所属栏目:百科 来源:网络整理
导读:我知道我可以使用pluck指定某些字段来查询数据库. ids = Item.where('due_at ?',2.days.from_now).pluck(:id) 但是我想知道,是否有一种方法可以指定某些字段,我希望避免从数据库中查询.某种反拔? posts = Post.where(published: true).do_not_lookup(:enorm
我知道我可以使用pluck指定某些字段来查询数据库.

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

但是我想知道,是否有一种方法可以指定某些字段,我希望避免从数据库中查询.某种反拔?

posts = Post.where(published: true).do_not_lookup(:enormous_field)

解决方法

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

像这样的东西:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读