Class.where(obj:"someone") 与 index.html.erb

Class.where(obj:"someone") with index.html.erb

我的方法有点问题。 我有:

model group title:string 
model user name:string user:references 

index.html.erb 中,我尝试使用 <=% User.where(group:"Admins) %>,然后在 localhost/users 上使用 return(我猜是 RAM 中的地址)- #<Project:0x007f6f38594af8>. 怎么可能我只得到类别是管理员的用户名?

UPD:我需要列出 "Admins" 组的所有用户。

是的,清除你的问题。你不能做 <=% User.where(group:"Admins") %> 因为 Group 是一个模型。

但您可以在模型中设定范围,这将为您提供 "Admins" 组

的所有用户列表
class User < ActiveRecord::Base
  scope :admin_groups, -> {includes(:groups).where(groups: { title: 'Admin') } }
end

在您看来您可以调用<%= User.admin_groups %>

希望这对您有所帮助。