ActiveRecord select except columns

后端 未结 4 781
栀梦
栀梦 2020-12-15 06:54

Is there a way I can specify to select ALL columns in ActiveRecord except just a few. For example, for a User, I don\'t want to select their password hash or their email. Is

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 07:22

    In some cases, particularly when you're considering a default_scope to exclude certain columns, I would advise against this approach because of a known issue with Rails about count breaking after a select clause. This can lead to surprising downstream errors.

    In such a case, consider instead breaking your record into two tables, one with the essential data, the other one with the data you only need from time-to-time, then use associations to access the extra data.

提交回复
热议问题