为没有角色的用户创建范围(rolify)

时间:2015-02-19 18:48:38

标签: ruby-on-rails scope rolify

我无法显示User模型中的照片。

我想仅向没有分配角色的用户展示照片。

所以,我需要为它创建一个查询:

@photos = Photo.where(User.find_by(Role == nil))

我无法让范围发挥作用......

有人可以尝试一下吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

让所有用户都没有角色:

User.with_role(nil)

因此,假设Photo belongs_to:user(照片表应该有user_id列):

@photos = Photo.where(user_id: User.with_role(nil))
相关问题