如何在 Rails 3 中访问具有特定角色的用户? [英] How do I access a user with a specific role in Rails 3?
问题描述
我为用户提供了三种模型.用户、角色和任务.这是模型的外观:
assignment.rb# == 架构信息# 架构版本:20101117094659## 表名:作业## id :整数不为空,主键# created_at :datetime# 更新时间:日期时间# user_id :整数# role_id :整数#班级分配:作业结尾用户名# == 架构信息# 架构版本:20110102225945## 表名:users## id :整数主键# 电子邮件:字符串(255)#encrypted_password :string(128)# password_salt :string(255)# reset_password_token :string(255)# remember_token :string(255)# remember_created_at :datetime# sign_in_count :整数# current_sign_in_at :datetime# last_sign_in_at :datetime# current_sign_in_ip :string(255)# last_sign_in_ip :string(255)# created_at :datetime# 更新时间:日期时间# 用户名:字符串(255)# f_name :string(255)# l_name :string(255)#类用户:作业def role_symbols角色.map做|角色|role.name.underscore.to_sym结尾结尾结尾
在我看来,要为当前用户选择项目,我这样做:
在我的项目控制器中,我有:
def 索引@projects = current_user.projectsresponse_to do |格式|format.html # index.html.erbformat.xml { 渲染:xml =>@项目}结尾
结束
然后在视图中我这样做:
<% if current_user.projects.exists?%><div class="数据">有 <%= current_user.projects.count %><br/><表格><% current_user.projects.each do |project|%><td><%= link_to project.name, project %></td></tr><%结束%><%其他%><div class="no-data"><%= image_tag('create-project-icon.png') %><br/>创建项目
<%结束%>
用户有 4 个角色:设计师、客户、管理员、超级用户.
每个设计师可以有多个客户.每个客户也可以属于多个设计师.
所以我想我有两个问题:
- 如果当前登录的用户(设计师)想要添加一个客户(他们只能添加客户,不能添加其他用户类型),我该怎么做?根据上面的代码示例,我将使用什么语法.添加客户后,我希望将他与项目、阶段、上传和评论相关联.因此,理论上,一名设计师将拥有多个项目,这些项目将属于多个客户.
- 然后我如何只为已登录的设计者检索客户端.即,我如何选择具有与 current_user 关联的角色客户端的所有用户?
谢谢.
解决方案 问题是...鉴于已经添加了用户(客户端)到另一个用户(设计师)拥有的项目,我如何检索那些用户
这很简单:
# 给定一个项目 @project = Project.find(id)@clients = @project.users.joins(:roles).where("roles.name = 'client'")
I have three models for users. User, role & assignments. This is how the models look:
assignment.rb
# == Schema Information
# Schema version: 20101117094659
#
# Table name: assignments
#
# id :integer not null, primary key
# created_at :datetime
# updated_at :datetime
# user_id :integer
# role_id :integer
#
class Assignment < ActiveRecord::Base
belongs_to :role
belongs_to :user
end
role.rb
# == Schema Information
# Schema version: 20101117094659
#
# Table name: roles
#
# id :integer not null, primary key
# name :string(255)
# created_at :datetime
# updated_at :datetime
#
class Role < ActiveRecord::Base
has_many :assignments
has_many :users, :through => :assignments
end
user.rb
# == Schema Information
# Schema version: 20110102225945
#
# Table name: users
#
# id :integer primary key
# email :string(255)
# encrypted_password :string(128)
# password_salt :string(255)
# reset_password_token :string(255)
# remember_token :string(255)
# remember_created_at :datetime
# sign_in_count :integer
# current_sign_in_at :datetime
# last_sign_in_at :datetime
# current_sign_in_ip :string(255)
# last_sign_in_ip :string(255)
# created_at :datetime
# updated_at :datetime
# username :string(255)
# f_name :string(255)
# l_name :string(255)
#
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable, and :lockable
devise :database_authenticatable, :registerable, :timeoutable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me
has_and_belongs_to_many :projects
has_many :stages
has_many :uploads
has_many :comments
has_many :assignments
has_many :roles, :through => :assignments
def role_symbols
roles.map do |role|
role.name.underscore.to_sym
end
end
end
In my view, to select the projects for the current user, I do this:
In my projects controller, I have:
def index
@projects = current_user.projects
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @projects }
end
end
Then in the view I do this:
<% if current_user.projects.exists? %>
<div class="data">
There are <%= current_user.projects.count %> projects.<br />
<table>
<% current_user.projects.each do |project| %>
<tr class="changer">
<td><%= link_to project.name, project %></td>
</tr>
<% end %>
</table>
</div>
<% else %>
<div class="no-data">
<%= image_tag('create-project-icon.png') %><br />
Create Project
</div>
<% end %>
The users have 4 roles: Designer, Client, Admin, Superuser.
Each designer can have multiple clients. Each client can also belong to multiple designers.
So I guess I have two questions:
- If the currently logged in user (a designer) wants to add a client (they can only add clients, no other user type), how do I do that? What's the syntax I will use according to my code samples above. Once I add a client, I will want to associate him to projects, stages, uploads and comments. So in theory, 1 designer will have multiple projects which will belong to multiple clients.
- How do I then retrieve the clients only, for the designer logged in. i.e. how do I select all the users with role client associated with the current_user ?
Thanks.
解决方案
The question was...given that users (clients) have already been added
to a project owned by another user (a designer), how do I retrieve
those users
That is pretty straight forward :
# Given a project @project = Project.find(id)
@clients = @project.users.joins(:roles).where("roles.name = 'client'")
这篇关于如何在 Rails 3 中访问具有特定角色的用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
登录
关闭
扫码关注1秒登录
发送“验证码”获取
|
15天全站免登陆