Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Records being cached with has_one associations #491

Open
mtclimberguy opened this issue May 5, 2020 · 0 comments
Open

Records being cached with has_one associations #491

mtclimberguy opened this issue May 5, 2020 · 0 comments
Labels

Comments

@mtclimberguy
Copy link

My team and I noticed that has_one associations are remaining cached after any calls to destroy the associated model. has_many calls are behaving as expected.

For example, a User 'has_many :addresses and has_one :main_address in our schema (MainAddress is a subclass of Address).

Here is an RSpec to help reproduce precisely.

RSpec.describe User, type: :model do
  let(:user) { create(:user) }
  let(:address) { create(:address, user_id: user.id, type: "MainAddress")}
  it 'an address should not return if it has been soft-deleted' do
    user.addresses << address
    expect(user.main_address.user_id).to eq(user.id)
    user.main_address.really_destroy! # or user.main_address.destroy!
    expect(user.addresses.count).to eq(0) # this expectation passes on the has_many
    expect(user.main_address).to be_nil # this expectation fails and still returns a MainAddress (with deleted_at filled in)
  end
end

We have to call reload on the parent model to get it to work. (i.e., user.reload) Then user.main_address return nil as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants