We've moved discussions to Discord

Adding new associations involving the Teams model.

Bryce Telfer
I'm working on a Jumpstart Pro app and trying to add a new AR relationship to the Teams model.
I have an Organisations model,  which I would like to have a has_many relationship with Teams:    Organisation has_many Teams,  Team belongs_to an Organisation.
So far so good:
class Organisation < ApplicationRecord
  has_many :billing_accounts
  has_many :teams
end

class Team < ApplicationRecord ... belongs_to :owner, class_name: "User" has_many :team_members, dependent: :destroy has_many :users, through: :team_members belongs_to :organisation end

Where I get stuck is in creating a new User, because it automagically creates a new personal team.  I assume in Jumpstart there is some logic that creates that new personal team for each new user, but I don't know where that is coming from or how to pass the organisation id to/through it.
Alain Pilon
Check the user_team concern:

  included do
    .....
    # Regular users should get their team created immediately
    after_create :create_personal_team
    after_update :update_personal_team
  end
  def create_personal_team
    # Invited users don't have a name immediately, so we will run this method twice for them
    # once on create where no name is present and again on accepting the invitation
    return unless name.present?
    return if personal_team.present?

    team = build_personal_team name: name, personal: true
    team.team_members.new(user: self, admin: true)
    team.save!
    team
  end
Bryce Telfer
Cool,  thanks for the pointer Alain
Alain Pilon
small advice: what every code you need to add, wrap it in a function so that there is only one line change in the existing code base, would make it easier to merge updates in the future!
Notifications
You’re not receiving notifications from this thread.