Skip to content

Instantly share code, notes, and snippets.

@staycreativedesign
Created October 28, 2017 21:00
Show Gist options
  • Select an option

  • Save staycreativedesign/b0695cadffb061c3416f01f5225b22d9 to your computer and use it in GitHub Desktop.

Select an option

Save staycreativedesign/b0695cadffb061c3416f01f5225b22d9 to your computer and use it in GitHub Desktop.
class User < ApplicationRecord
has_many :sponsored_teams, class_name: 'Team', foreign_key: "sponsor_id",
inverse_of: :sponsor, before_add: :check_to_if_sponsor_count
def check_to_if_sponsor_count(new_team)
if new_team.sponsor.sponsored_teams.count >= 4
add_team_to_new_sponsor(new_team)
end
end
def add_team_to_new_sponsor(new_team)
u = new_team.sponsor
new_sponsor = u.sponsored_teams.first.coach
new_team.sponsor = new_sponsor
new_team.save
new_sponsor.sponsored_teams << new_team
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment