Steve Q
Steve Q

Reputation: 395

Teams of users rails app

I need to create teams of users. A user belongs to a team (only one team) a team has many users. I can't figure out how to get a user to be able to create, join, and leave a team. Below is what I have so far, but I'm sure I'm doing something terribly (and "newby" wrong).

User Model:

belongs_to :teams, dependent: :destroy

def team_member?(team)
    team_relationships.find_by_team_id(team.id)
end

def join!(team)
    team_relationships.create!(team_id: team.id)
end  

def unjoin!(team)
    team_relationships.find_by_team_id(team.id).destroy
end

team model

has_many :users, through: :team_relationships, dependent: :destroy

attr_accessible :team_name, :team_id

validates :user_id, presence: true
validates :team_name, presence: true, length: { maximum: 140 }

default_scope order: 'teams.created_at DESC'

team_relationship model

attr_accessible :team_id

belongs_to :team
belongs_to :user

validates :team_id, presence: true  

routes:

  resources :teams do
    member do
      get 'join'
      get 'leave'
    end
  end

teams_controller:

def join
  @team = Team.find params[:team_id]
  current_user.update_attribute(:team_id, @team.id)
  redirect_to @user
end

def leave
  @team = Team.find params[:id]
  current_user.update_attribute(:team_id, nil)
  redirect_to @user
end

_join_team.html.erb

<%= form_for(current_user.team_relationships.build(team_id: @team_id),
             remote: true) do |f| %>
  <div><%= f.hidden_field :team_id %></div>
  <%= f.submit "Join", class: "btn btn-large btn-primary" %>
<% end %>

_unjoin_team.html.erb

<%= form_for(current_user.team_relationships.find_by_team_id(@team_id),
         html: { method: :delete }) do |f| %>
  <%= f.submit "Leave Team", class: "btn btn-large" %>
<% end %>

If you can't tell I was trying to adapt some of what's in Hartl's tutorial for this purpose. What am I doing wrong?

I believe I have gotten the models figured out, but now I'm not sure how to get a user to create a team, destroy a team, join a team, or leave a team. What do I have to do in the models, controllers, and views to make it happen?

Upvotes: 1

Views: 1362

Answers (3)

Steve Q
Steve Q

Reputation: 395

I figured it out. The build method wouldn't work. The team was already established, and the user was already established. I just needed to establish the connection. Here's what I have:

Teams Controller:

def show
  @team =  Team.find(params[:id])
  @team_members = @team.users
  @user = User.find(params[:id]) if signed_in?
end

Join Button Partial:

<%= form_for(@user) do |f| %>
  <%= f.hidden_field :team_id, :value => @team.id %>
  <%= f.submit "Join Team", class: "btn btn-large btn-primary" %>
<% end %>

Upvotes: 2

Pritesh Jain
Pritesh Jain

Reputation: 9146

When you have only has one relation ship you do not need has_many , through as user belongs_to only one teams ans team has many users its a has_many -> belongs_to relationship

User Model:

belongs_to :team

validates :user_id, presence: true

def team_member?
    team.present?
end

def join!(team)
  return false if team_member?
  team.create!(team_id: team.id)
end  

def unjoin!
  return if team_member?
  team.destroy
end

Team model

has_many :users

attr_accessible :team_name, :team_id

validates :team_name, presence: true, length: { maximum: 140 }

default_scope order: 'teams.created_at DESC'

TeamRelationship model #NOT NEEDED

_join_team.html.erb

<%= button_to "Join", join_teams_path(current_user.team_build(team_id: @team_id),  class: "btn btn-large btn-primary", remote: true %>

_unjoin_team.html.erb

<%= button_to "Leave Team", leave_teams_path(current_user.team) , class: "btn btn-large btn-primary" , remote: true  %>

team_controllor

 def join
   @team = Team.find params[:id]
   if current_user.join!(@team.id)
     redirect_to @user #NOTE dont use redirect when you perform actions with ajax.
   else
     #render some error
   end
 end

def leave
  if current_user.unjoin!
     redirect_to @user #NOTE dont use redirect when you perform actions with ajax.
   else
     #render some error
   end
end

I suggest to you go through some docs from rails they are well defined

http://guides.rubyonrails.org/ajax_on_rails.html

http://railscasts.com/episodes/205-unobtrusive-javascript

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html

http://guides.rubyonrails.org/association_basics.html

Upvotes: 0

Peter Duijnstee
Peter Duijnstee

Reputation: 3779

Not sure if this is your problem but

belongs_to :teams

should be

belongs_to :team

Belongs to is always singular.

edit: It looks like you're going for a many to many relationship. Try this:

class User
  has_many :team_relationships, dependent: :destroy
  has_many :teams, through: :team_relationships
end

class Team
  has_many :team_relationships, dependent: :destroy
  has_many :users, through: :team_relationships
end

class TeamRelationship
  belongs_to :user
  belongs_to :team
end

I think that should do it.

Upvotes: 0

Related Questions