Skip to content

Incorrect counts and records returned for associated models #479

Open
@davismattg

Description

@davismattg

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions