在Rails和ActiveRecord中查询时忽略某个字段

时间:2015-03-03 13:13:59

标签: ruby-on-rails ruby ruby-on-rails-4 activerecord

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

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

但是我想知道,是否有办法指定某些字段我想避免从db中查询。某种反拔?

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

1 个答案:

答案 0 :(得分:7)

Model#attribute_names应返回列/属性数组。您可以排除部分内容并转到pluckselect方法。

这样的事情:

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