在Rails和ActiveRecord中查询时忽略某些字段
我知道我可以指定某些字段来使用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'])