<file_sep>class CreateTeamsCardsJoinTable < ActiveRecord::Migration[5.1]
  def change
  	create_join_table :teams, :cards
  end
end
<file_sep>class Card < ApplicationRecord
	belongs_to :list
	has_and_belongs_to_many :teams
end
<file_sep>class ListSerializer < ActiveModel::Serializer
  attributes :id, :name, :cards

  def cards
  	cards = object.cards.map do |c| 
  		{
  			id: c.id,
  			teams: teams(c),
  			title: c.title
  		} 
  	end

  	return cards
  end

  def teams(card)
    teams = card.teams.map { |t| {id: t.id, name: t.name} }
  end
end<file_sep>class Team < ApplicationRecord
	has_and_belongs_to_many :cards
end
<file_sep>class List < ApplicationRecord
	has_many :cards
end
<file_sep>class CardSerializer < ActiveModel::Serializer
  attributes :id, :title, :teams

  def teams
    teams = object.teams.map { |t| {id: t.id, name: t.name} }
  end
end<file_sep>Rails.application.routes.draw do
    namespace :api do
    namespace :v1 do
      resources :teams
      resources :cards do
        member do
          get 'add_team/:team_id', to: "cards#add_team"
          get 'rem_team/:team_id', to: "cards#rem_team"
        end
      end
      resources :posts
      resources :lists	
    end
  end

  match '*path', via: [:options], to: lambda {|_| [204, { 'Content-Type' => 'text/plain' }]}

  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
<file_sep>class Api::V1::CardsController < ApplicationController
  before_action :set_card, only: [:show, :update, :destroy, :add_team, :rem_team]
  before_action :set_team, only: [:add_team, :rem_team]
  # GET /cards
  def index
    @cards = Card.all

    render json: @cards
  end

  # GET /cards/1
  def show
    render json: @card
  end

  # POST /cards
  def create
    @card = Card.new(card_params)

    if @card.save
      render json: @card, status: :created
    else
      render json: @card.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /cards/1
  def update
    if @card.update(card_params)
      render json: @card
    else
      render json: @card.errors, status: :unprocessable_entity
    end
  end

  def add_team
    @card.teams << @team
    render json: @card, status: :created
  end

  def rem_team
    @card.teams = @card.teams.select{|t| !t.eql?(@team)}
    render json: @card, status: :created
  end

  # DELETE /cards/1
  def destroy
    @card.destroy
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_card
      @card = Card.find(params[:id])
    end

    def set_team
      @team = Team.find(params[:team_id])
    end

    # Only allow a trusted parameter "white list" through.
    def card_params
      params.require(:card).permit(:title, :list_id)
    end
end
