Skip to content

Instantly share code, notes, and snippets.

@Tonkonozhenko
Created May 17, 2016 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tonkonozhenko/95438d1b8f33e137ff706164eb69cd38 to your computer and use it in GitHub Desktop.
Save Tonkonozhenko/95438d1b8f33e137ff706164eb69cd38 to your computer and use it in GitHub Desktop.
class A < ActiveRecord::Base
has_and_belongs_to_many :bs
end
class B < ActiveRecord::Base
has_and_belongs_to_many :as
end
class Ab < ActiveRecord::Base
self.table_name = :as_bs
end
RSpec.describe A, type: :model do
let(:n) { 10 }
it do
a = A.create
a.bs = n.times.map { B.create }
a.save
expect(a.bs.count).to eq(n)
expect(B.count).to eq(n)
expect(Ab.count).to eq(n)
expect do
expect do
a.bs.destroy_all
end.not_to change(B, :count)
end.to change(Ab, :count).by(-n)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment