Currently, I get two tables :
Users (with a foreign_key : team_id)
Teams
Relationships : one-to-many (a user can have only 1 team, but a team car have many users)
Users : belongs_to :teams
Teams: has_many :users
class Team < ActiveRecord::Base
has_many :users
class User < ActiveRecord::Base
belongs_to :teams
config/routes.rb
get "users/new"
resources :users
resources teams do
member do
get 'join'
get 'leave'
end
end
resources :sessions, only: [:new, :create, :destroy]
match '/signin', to: 'sessions#new'
match '/signout', to: 'sessions#destroy', via: :delete
match '/users', to: 'users#index'
match '/teams', to: 'teams#index'
end
And I try to make a button on the team page (e.g. myapp/teams/1
) where a user (who call current_user
) can join or leave this team.
To join a team we just have to update the column user.team_id
and put on this the id of the team (to leave a team, the column user.team_id
need to be empty).
Anyone have an idea to make these two buttons?