Home > Back-end >  How can show only those user who is not linked with project in rails
How can show only those user who is not linked with project in rails

Time:09-21

I am trying to show only those developers who are not linked with project but it always show one developer who is already linked

ProjectDeveloper is model which have project and developer id User have a column user type which type is enum who contain developer manager and qa

controller

 def index

    @users = User.where(user_type: 'Developer')
    @project_developer = ProjectsDeveloper.where.not(id: @users.ids)
    project_id = params['project_id'].to_i
    @project_id = Project.find(project_id)
 end

html.erb

<% @project_developer.each do |user| %>
   <h3><%=user.developer_id %></h3>

   <%=button_to 'Assign' , projects_developer_index_path({project_id: @project_id.id, developer_id: user.id}),
         method: :post %><br>
<% end %>

CodePudding user response:

Try this way:

def index
    @project = Project.find(params['project_id'].to_i)
    @project_developer = project.projects_developers.pluck(:developer_id)
    @users = User.where(user_type: 'Developer').where.not(id: @project_developer)
 end

And in view use @users instance to display developers.

<% @users.each do |user| %>
   <h3><%=user.id %></h3>

   <%=button_to 'Assign' , projects_developer_index_path({project_id: @project_id.id, developer_id: user.id}),
         method: :post %><br>
<% end %>
  • Related