我无法显示User
模型中的照片。
我想仅向没有分配角色的用户展示照片。
所以,我需要为它创建一个查询:
@photos = Photo.where(User.find_by(Role == nil))
我无法让范围发挥作用......
有人可以尝试一下吗?
谢谢!
答案 0 :(得分:3)
让所有用户都没有角色:
User.with_role(nil)
因此,假设Photo belongs_to:user(照片表应该有user_id列):
@photos = Photo.where(user_id: User.with_role(nil))