Created
March 4, 2021 19:57
-
-
Save bpo/6f568b1fe79e80b0d7e0391eca07085c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile(true) do | |
source 'https://rubygems.org' | |
gem 'rails', '~> 5.2.0' | |
gem 'sqlite3' | |
end | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveSupport::Deprecation.debug = true | |
ActiveRecord::Schema.define do | |
create_table :teams do |t| | |
t.timestamps | |
end | |
create_table :users do |t| | |
t.belongs_to :team | |
t.string :level, :null => false | |
t.timestamps | |
end | |
end | |
class Team < ActiveRecord::Base | |
has_many :users | |
has_many :owners, -> { where level: "owner" }, | |
class_name: "User" | |
end | |
class User < ActiveRecord::Base | |
attribute :level, default: 'member' | |
end | |
class ExampleTest < Minitest::Test | |
def test_add_users_to_team | |
team = Team.create | |
member = team.users.create | |
assert_equal "member", member.level | |
end | |
def test_add_owners_to_team | |
team = Team.create | |
owner = team.owners.create | |
assert_equal "owner", owner.level # fails | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment