ActiveRecord 选择列除外 [英] ActiveRecord select except columns

查看:19
本文介绍了ActiveRecord 选择列除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以指定选择 ActiveRecord 中的所有列,只有少数列除外.例如,对于用户,我不想选择他们的密码哈希或电子邮件.这是可能的还是我必须手动对所有列进行硬编码?

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 this possible or do I have to manually hardcode all columns?

谢谢

推荐答案

像这样写一个作用域

def select_without columns
  select(column_names - columns.map(&:to_s))
end

这篇关于ActiveRecord 选择列除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆