Skip to content

Instantly share code, notes, and snippets.

@staycreativedesign
Created October 11, 2017 19:39
Show Gist options
  • Save staycreativedesign/3c63688a68a846162f9c534dc82661a6 to your computer and use it in GitHub Desktop.
Save staycreativedesign/3c63688a68a846162f9c534dc82661a6 to your computer and use it in GitHub Desktop.
class Team < ApplicationRecord
has_many :users
has_many :business_industries, through: :users
belongs_to :coach, class_name: 'User'
belongs_to :leader, class_name: 'User', optional: true
belongs_to :captain, class_name: 'User', optional: true
belongs_to :social_director, class_name: 'User', optional: true
belongs_to :trainer, class_name: 'User', optional: true
belongs_to :sponsor, class_name: 'User', optional: true
validates_associated :sponsor
end
require 'rails_helper'
RSpec.describe Team, :type => :model do
let(:admin) { create(:user, role: Role::Admin) }
before(:each) do
create_list(:team, 4, sponsor: admin)
end
it "doesn't allow for more than 4 teams for a sponsor" do
admin.reload
admin.sponsored_teams << build(:team)
admin.save
binding.pry
expect(admin.sponsored_teams.count).to eq(4)
end
end
class User < ApplicationRecord
has_many :sponsored_teams, class_name: 'Team', foreign_key: "sponsor_id"
validates_length_of :sponsored_teams, maximum: 4
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment