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

Incorrect counts and records returned for associated models #479

Open
davismattg opened this issue Oct 18, 2019 · 2 comments
Open

Incorrect counts and records returned for associated models #479

davismattg opened this issue Oct 18, 2019 · 2 comments
Labels

Comments

@davismattg
Copy link

davismattg commented Oct 18, 2019

Rails 5.1.6, Ruby 2.4.3, paranoia 2.4.1. MySQL 5.7

I have the following model setup:

class Parent < ApplicationRecord
    acts_as_paranoid
    has_many :children, dependent: :destroy
    accepts_nested_attributes_for :children, reject_if: :all_blank, allow_destroy: true
    ...
end
class Child < ApplicationRecord
    acts_as_paranoid
    belongs_to :parent
    ...
end

I'm seeing issues when trying to do get the associated children records for a given parent:

parent = Parent.find(1)

One record in the associated_children collection is soft-deleted, but the others are not.

parent.children.count 
=> 0 # returns 0 even though I can see 32 non-deleted associated_children records in the DB and Rails console

parent.children.with_deleted.count
=> 33

So there should be 32 records returned for associated_children, but instead I see:

parent.children.without_deleted.count
=> 0

parent.children.with_deleted.pluck(:deleted_at)
=> [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, Thu, 03 Jan 2019 16:56:33 UTC +00:00, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]

The kicker is this is only happening on 1 table of my database, and only on production. When I copy the production database locally the exact same queries as above work just fine for the same models that are failing on production.

@davismattg
Copy link
Author

I've found that I can workaround the issue by changing my Child model to the following:

class Child < ApplicationRecord
    acts_as_paranoid without_default_scope: true
    default_scope {where(deleted_at: [nil, ''])}
    ...
end

Any idea why that would be?

@duybn-0885
Copy link

Do you use recursive delete ?

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

3 participants