forgeplus/app/models/team_user.rb

39 lines
959 B
Ruby
Raw Permalink Normal View History

2021-01-11 17:44:11 +08:00
# == Schema Information
#
# Table name: team_users
#
# id :integer not null, primary key
# organization_id :integer
# team_id :integer
# user_id :integer
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_team_users_on_organization_id (organization_id)
# index_team_users_on_team_id (team_id)
# index_team_users_on_user_id (user_id)
#
class TeamUser < ApplicationRecord
belongs_to :organization
belongs_to :team, counter_cache: :num_users
belongs_to :user
2021-12-22 11:16:27 +08:00
has_one :member
2021-01-15 14:55:52 +08:00
validates :user_id, uniqueness: {scope: [:organization_id, :team_id]}
2021-12-22 11:16:27 +08:00
before_destroy :remove_project_member
2021-01-11 17:44:11 +08:00
def self.build(organization_id, user_id, team_id)
self.find_or_create_by!(organization_id: organization_id, user_id: user_id, team_id: team_id)
2021-01-11 17:44:11 +08:00
end
2021-12-22 11:16:27 +08:00
def remove_project_member
member.destroy if member.present?
end
2021-01-11 17:44:11 +08:00
end