How to access a user with a specific role in Rails 3?

I have three models for users. User, role and tasks. Here's what the models look like:

    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 opinion, to select projects for the current user, I do this:

In my project 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 %>

Users have 4 roles: Designer, Client, Admin, Superuser.

Each designer can have several clients. Each client can also belong to several designers.

So, I have two questions:

  • () ( , ), ? . , , , . , .
  • , . , , current_user?

.

0
1

... , () , (),

:

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

Source: https://habr.com/ru/post/1736241/


All Articles