I have three models for users. User, role and tasks. Here's what the models look like:
assignment.rb
class Assignment < ActiveRecord::Base
belongs_to :role
belongs_to :user
end
role.rb
class Role < ActiveRecord::Base
has_many :assignments
has_many :users, :through => :assignments
end
user.rb
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable, :timeoutable,
:recoverable, :rememberable, :trackable, :validatable
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
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?
.