0
我有三種模式供用戶使用。用戶,角色&分配。這是該機型的外觀:如何在Rails 3中訪問具有特定角色的用戶?
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
在我看來,要選擇針對當前用戶的項目,我這樣做:
在我的項目控制,我有:
def index
@projects = current_user.projects
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @projects }
end
結束
然後在視圖我這樣做:
<% 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 %>
用戶有4個角色:設計者,客戶端,管理員,超級用戶。
每個設計師可以有多個客戶端。每個客戶也可以屬於多個設計師。
所以我想我有兩個問題:
- 如果當前登錄的用戶(設計師)想要增加客戶(他們只能添加客戶端,其他用戶類型),我該怎麼辦那?根據我上面的代碼示例,我將使用什麼語法。一旦我添加了一個客戶端,我會希望將他與項目,階段,上傳和評論相關聯。所以從理論上講,1名設計師將擁有多個屬於多個客戶的項目。
- 然後,我如何才能檢索客戶端,以便設計人員登錄。即,如何選擇所有具有與current_user關聯的角色客戶端的用戶?
謝謝。
你的意思是你如何限制它,以便設計者只能添加一個客戶端?我想你是從另一個問題的評論中使用declarative_authorization,但是這個問題並不清楚。此外,在用戶之間似乎也沒有關聯,允許您將用戶添加爲設計的客戶端,除非這是通過其他未指定的關聯。這可能只是我,但我不覺得這裏有足夠的清晰度來回答這個問題。 – Shadwell 2011-01-09 21:41:31
對於沙德威爾來說,沒有更多的不明確的聯想。我正試圖弄清楚如何進步。那麼,關於設計師只添加一個客戶端的問題是一個很好的問題。我正在假設設計師添加的任何人(即任何用戶)將始終具有「客戶」角色。但不,這不是問題。問題是......鑑於用戶(客戶)已經被添加到另一個用戶(設計者)擁有的項目中,我如何檢索這些用戶。 – marcamillion 2011-01-10 00:48:03