如何 select 有附件的记录

How to select records that have attachments

如果我的模型 "User" 带有附件 "avatar",我如何 select 所有具有头像的用户?

即,我想做类似的事情:

users_with_avatars = User.where(avatar: true)

由于附件通常是文件的字符串,所以我认为,选择可以用否定来完成。对于 carrierwave 你可以搜索挂载的属性:

users_with_avatars = User.where.not(avatar: nil)

对于 paperclip 您可以搜索 avatar_file_name 字段:

users_with_avatars = User.where.not(avatar_file_name: nil)

avatar_file_size以上字段:

users_with_avatars = User.where("avatar_file_size > 0")