2011-01-07 2 views
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 %> 
0,123,516 :

그런 다음보기에 내가 이렇게 끝

사용자에게는 Designer, Client, Admin, Superuser의 4 가지 역할이 있습니다.

각 디자이너는 여러 클라이언트를 가질 수 있습니다. 각 클라이언트는 여러 디자이너에게 속할 수도 있습니다.

그래서 나는 두 가지 질문이 추측 : 현재 로그인 한 사용자 (디자이너)가 (그들은 단지, 다른 사용자 유형을 클라이언트를 추가 할 수 없습니다) 클라이언트를 추가하고자하는 경우

  1. 을 어떻게합니까 그? 위의 코드 샘플에 따라 사용할 구문은 무엇입니까? 일단 고객을 추가하면 프로젝트, 스테이지, 업로드 및 댓글과 연관시켜야합니다. 따라서 이론적으로 1 명의 디자이너는 여러 클라이언트에 속할 여러 프로젝트를 갖게됩니다.
  2. 로그인 한 디자이너에 대해서만 클라이언트를 검색하려면 어떻게해야합니까? 즉, current_user와 연결된 역할 클라이언트가있는 모든 사용자를 선택하려면 어떻게해야합니까?

감사합니다.

+0

디자이너가 클라이언트 만 추가 할 수 있도록 어떻게 제한합니까? 난 당신이 다른 질문에 대한 의견에서 declarative_authorization를 사용하고 있다고 생각하지만,이 질문에서 분명하지 않다. 또한 지정되지 않은 다른 연결을 통해 사용자가 아닌 한 사용자를 디자인의 클라이언트로 추가 할 수있는 사용자 간의 연결이없는 것 같습니다. 그것은 나일지도 모르지만 질문에 대답하기에 충분한 명확성이 여기에있는 것처럼 느껴지지 않습니다. – Shadwell

+0

오른쪽 Shadwell에서 더 이상 불특정 단체가 없습니다. 진행 방법을 알아 내려고 노력 중입니다. 음, 디자이너를 추가하는 것만으로 클라이언트를 추가하는 것은 매우 좋은 질문입니다. 저는 디자이너가 추가 한 사람 (즉, 모든 사용자)이 항상 '클라이언트'역할을 담당한다는 가정하에 작업하고있었습니다. 그러나 아니, 그것은 질문이 아니었다. 문제는 ... 사용자 (클라이언트)가 이미 다른 사용자 (디자이너)가 소유 한 프로젝트에 추가 된 경우 해당 사용자를 검색하는 방법입니다. – marcamillion

+0

그래서 생각해 보면, 나는 역할 클라이언트가있는 4 명의 사용자가있는 프로젝트 A를 가지고있다. SELECT * FROM users where role = 'client'& Project = 'A'' 어떻게 할 수 있습니까? SQL 문을 사용하지 않고. 이상적으로는 Ruby/Rails 코드 만 사용합니다. 나는 decl_auth & devise를 사용하여 인증합니다. 당신의 생각을 듣고 싶습니다. – marcamillion

답변

0

이 문제는 ... 사용자 (클라이언트)가 이미 다른 사용자 (디자이너)가 소유 한 프로젝트에 을 추가되었는지 주어졌다, 어떻게

즉 이러한 사용자 를 검색 할 꽤 똑바로 앞으로 :

# Given a project @project = Project.find(id) 
@clients = @project.users.joins(:roles).where("roles.name = 'client'") 
+0

시련의 성공 여부는 무엇입니까? – charlysisto

관련 문제