开发者

How do I access a user with a specific role in Rails 3?

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 %>
  开发者_如何学Go                  <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:

  1. 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.
  2. 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'")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜