Skip to content

Instantly share code, notes, and snippets.

@bpo
Created March 4, 2021 19:57
Show Gist options
  • Save bpo/6f568b1fe79e80b0d7e0391eca07085c to your computer and use it in GitHub Desktop.
Save bpo/6f568b1fe79e80b0d7e0391eca07085c to your computer and use it in GitHub Desktop.
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