Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# frozen_string_literal: true
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'activerecord', '6.0.3.4'
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)
ActiveRecord::Schema.define do
create_table :users, force: true
create_table :teams, force: true
create_table :team_memberships, force: true do |t|
t.integer :team_id
t.integer :user_id
t.index %i[team_id user_id]
end
create_table :posts, force: true do |t|
t.string :title, null: false
t.integer :user_id
t.index :user_id
end
end
class Team < ActiveRecord::Base
has_many :team_memberships
has_many :users, through: :team_memberships
end
class User < ActiveRecord::Base
has_many :team_memberships
has_many :teams, through: :team_memberships
has_many :posts
accepts_nested_attributes_for :posts
end
class TeamMembership < ActiveRecord::Base
belongs_to :team
belongs_to :user
end
class Post < ActiveRecord::Base
belongs_to :user
accepts_nested_attributes_for :user
end
class BugTest < Minitest::Test
def test_association_stuff
team = Team.create!
user = User.create!(
posts: [
Post.new(title: 'dummy title')
],
team_ids: [team.id]
)
assert_equal 1, user.teams.count
end
end
@r7kamura
Copy link
Author

r7kamura commented Nov 6, 2020

  1) Failure:
BugTest#test_association_stuff [-:73]:
Expected: 1
  Actual: 2

1 runs, 1 assertions, 1 failures, 0 errors, 0 skips

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment