acts_as_paranoid icon indicating copy to clipboard operation
acts_as_paranoid copied to clipboard

ActiveRecord queries with :includes do not respect :with_deleted => true

Open moottoast opened this issue 12 years ago • 10 comments

I can't see a way around the N+1 problem without using :includes in my Active Record queries. Per the documentation:

class ParanoiacParent < ActiveRecord::Base
    has_many :children, :class_name => "ParanoiacChild"
end

class ParanoiacChild < ActiveRecord::Base
    belongs_to :parent, :class_name => "ParanoiacParent"
    belongs_to :parent_with_deleted, :class_name => "ParanoiacParent", :with_deleted => true
end

However if the following is called in a controller:

@paranoiac_children = ParanoiacChild.includes(:parent_with_deleted).all

The log load statement shows a "('paranoiac_parents'.'deleted_at' IS NULL)" in the WHERE clause resulting in undefined method errors. I guess I'm forced to avoid using :includes. Is this behavior by design?

moottoast avatar Apr 26 '12 18:04 moottoast

It doesn't work for me also. I don't get undefined method error but it just ignores the "with_deleted" option. I used a temporary dirty hack to get it working:

@projects = current_user.projects.includes(:users,:stages).where("users.deleted_at IS NOT NULL or users.deleted_at IS NULL")

vahtel avatar Jul 01 '12 08:07 vahtel

having the same problem. would be awesome if this was fixed - cheers!

paulkoegel avatar Oct 08 '12 13:10 paulkoegel

Even I have the same problem not sure how to go about it

perumal avatar Nov 06 '12 15:11 perumal

+1

pelargir avatar Mar 30 '13 14:03 pelargir

+1

chrismhilton avatar Apr 02 '13 10:04 chrismhilton

+1

tkupari avatar May 31 '13 06:05 tkupari

Just hit this today. +1

timscott avatar Nov 12 '13 18:11 timscott

+1

AknEp avatar Jun 30 '14 06:06 AknEp

+1

nfriend21 avatar Mar 31 '15 16:03 nfriend21

:+1:

mamantoha avatar Feb 23 '16 15:02 mamantoha