Learning how to create foreign keys at the database, I had these two models:
class Bar < ApplicationRecord
has_many :foos
end
# and
class Foo < ApplicationRecord
belongs_to :bar
end
Then, I created the following foreign key:
add_foreign_key :foos, :bars, on_delete: :cascade
Therefore, if I delete a bar
it should also delete the foos
that referenced it (and it really did). But I wanted to create a test for it, so I did this:
foo = create(:foo) # a factory that creates a foo with a bar parent
bar = foo.bar
bar.destroy
expect(foo).to_not be_persisted
And surprisingly it didn't work. And then instead of expect(foo).to_not be_persisted
I tried this just to be sure:
expect(Foo.all.size).to eq 0
expect(Foo.count).to eq 0
expect(Foo.all).to_not include foo
And it worked! So my question is: Why be_persisted?
didn't work? Shouldn't it verify if foo
is actually saved at the database?