[英]Ignoring certain field when querying in Rails and 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'])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.