Question: Why be_persisted? doesn't perceive database changes? on rails


Why be_persisted? doesn't perceive database changes? on rails

Answers 1
Added at 2016-12-27 01:12

Learning how to create foreign keys at the database, I had these two models:

class Bar < ApplicationRecord
  has_many :foos
# and
class Foo < ApplicationRecord
  belongs_to :bar

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 =
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?

Answers to

Why be_persisted? doesn&#39;t perceive database changes? on rails

nr: #1 dodano: 2016-12-27 21:12

You need to be aware that by using SQL cascade rules ActiveRecord won't be aware of it.

If you had specified in the model itself something like

has_many: :foos, dependent: :destroy

instead of using an SQL FK cascade rule .. then ActiveRecord would have cascaded the delete and been aware of it.

In your rspec test after doing bar.destroy do something like expect(Foo.find( be_nil instead to test the existence of the child record - or maybe foo.reload may give you a nil object too.

Source Show
◀ Wstecz