kevin lopez
kevin lopez

Reputation: 87

How to setup basic Rails models associations?

hey guys im working on a application where a devise user sign ups and logs in, Once the user logs in they can 'create a team' or 'join a team'. I have my associations set up like this

user.rb

class User < ApplicationRecord
   devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable, :confirmable 
   validates_presence_of :phone, :city, :state, :street, :zip, presence: true, on: :create

   belongs_to :team      
end

team.rb

class Team < ApplicationRecord
   has_many :users   
end

and my tables are set up

schema.rb

create_table "teams", force: :cascade do |t|
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string "team_name"
end

create_table "users", force: :cascade do |t|
  t.string "email", default: "", null: false
  t.string "encrypted_password", default: "", null: false
  t.string "reset_password_token"
  t.datetime "reset_password_sent_at"
  t.datetime "remember_created_at"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
  t.string "confirmation_token"
  t.datetime "confirmed_at"
  t.datetime "confirmation_sent_at"
  t.string "firstname"
  t.integer "team_id"
  t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
  t.index ["email"], name: "index_users_on_email", unique: true
  t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

team_controller.rb

class TeamController < ApplicationController
   before_action :authenticate_user!

   def index
     @team = current_user.team
   end

   def new_team

   end

   def create_team
     @team = current_user.create_team(sanitize_team)
     if @team.save 
       redirect_to team_root_path
     else
       render json: @team.errors.full_messages
     end
   end

   def join_team 
     @teams = Team.all
   end

   def team

   end

   private 

   def sanitize_team
     params.require(:team).permit(:team_name, :team_statement)
   end
end

I want the users 'team_id' attribute to update with the teams id when they create a team. or when they join a team. Are my associations correct? how would i make this happen in the controller ?

Upvotes: 1

Views: 61

Answers (3)

max
max

Reputation: 101811

Start by setting the association up as optional:

class User < ApplicationController 
  belongs_to :team, optional: true
end

Otherwise the validations on the user model will not let the user be saved without a team.

Then setup the teams resource:

# config/routes.rb
resources :teams do
  post :join
end

post :join creates an additional POST /teams/:team_id/join route.

Then setup the controller:

class TeamsController

  # ...

  # GET /teams/new
  def new
    @team = Team.find
  end

  # POST /teams
  def create
    @team = Team.new(team_params)
    if @team.save
      unless current_user.team
        current_user.update(team: @team)
      end
      redirect_to 'somewhere'
    else
      render :new
    end
  end

  # ...

  def join
    @team = Team.find(params[:team_id])
    if current_user.update(team: @team)
      redirect_to @team, notice: 'Team joined'
    else
      redirect_to @team, error: 'Could not join team'
    end
  end

  #

  private
    def team_params
      params.require(:team).permit(:team_name, :team_statement)
    end 
end

Note that prefixing your action names is neither needed nor compatible with the "Rails way". Prefixing column names is also largely superfluous.

Upvotes: 0

anothermh
anothermh

Reputation: 10526

Let's strip your code example down to the minimum required:

# app/models/team.rb
class Team < ApplicationRecord
  has_many :users
end

# app/models/user.rb
class User < ApplicationRecord
  belongs_to :team
end

# db/migrate/20181124230131_create_teams.rb
class CreateTeams < ActiveRecord::Migration[5.2]
  def change
    create_table :teams do |t|
      t.string :team_name
      t.timestamps
    end
  end
end

# db/migrate/20181124230136_create_users.rb
class CreateUsers < ActiveRecord::Migration[5.2]
  def change
    create_table :users do |t|
      t.belongs_to :team
      t.timestamps
    end
  end
end

Then in your controller:

team = Team.where(team_name: 'foo').first_or_create!
team.users << current_user

Upvotes: 1

DonPaulie
DonPaulie

Reputation: 2214

Yes, associations are correct. You can do it better only by adding foreign key to your database schema. It can be done by generator rails g migration AddTeamToUsers team:references

More information about associations can be found here: https://guides.rubyonrails.org/association_basics.html

In controller you have to change only the whitelisting params to allow team_id. And you probably need to add to your form in view something like this: <%= f.select :team_id, Team.all.map { |t| [t.team_name, t.id] } %>

Upvotes: 1

Related Questions