簡體   English   中英

在Rails和ActiveRecord中查詢時忽略某個字段

[英]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應該返回列/屬性的數組。 您可以排除其中一些並傳遞給pluckselect方法。

像這樣的東西:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM