I have a Project
model and Team
model and User
model through devise. The associations between the models are as follows:-
class Project
has_many :teams, :dependent => :destroy
end
class Team
belongs_to :project
belongs_to :user
end
class User
has_many :teams, :dependent => :destroy
end
Teams are nested withins Projects, so every project has its own seperate group of teams.
resources :projects do
resources :teams
end
The teams themselves are derived from Users, so when a new team is created, it is linked to the current_user that is signed in:
def create
@project = Project.find(params[:project_id])
@team = @project.teams.new(team_params)
@team.user = current_user
if @team.save
redirect_to @project, notice: 'Successfully Joined Project'
else
render action: 'new'
end
end
The Problem:-
I need the user to only have one Team per project (so he can only join a project once). I figured I could do this in the following way:
<% if @project.teams.user == current_user do %>
<%= link_to 'edit role', edit_project_team_path(@project), class: 'btn btn-primary' %>
<% end %>
<% else %>
<%= link_to 'Join Project', new_project_team_path(@project), class: 'btn btn-primary' %>
<% end %>
However im getting the following error for the previous code:
NoMethodError in Projects#show
undefined method `user' for #<Team::ActiveRecord_Associations_CollectionProxy:0x5858430>
Im not sure if the problem is with my associations or if im trying to achieve this the wrong way.
P.S think of Team as Team Member (as a team suggests a group of users however each team actually consists of one user only)