Rails acts_as_paranoid and has_many: through

So, I am using rails3_acts_as_paranoid gem and have some problems with area management with has_many: through associations.

for example

# User.rb acts_as_paranoid has_many :folders has_many :files, :through => :folders 

-

 # Folder.rb acts_as_paranoid belongs_to :user has_many :files, :dependent => :destroy 

-

 # File.rb acts_as_paranoid belongs_to :files 

Now let's just say somewhere in user_controller.rb. I want to request all files belonging to the user, regardless of whether they were deleted and / or belong to the folders that were deleted. So naturally, I would suggest doing something like the following

 current_user.files.with_deleted 

with_deleted method does work when deleting files.deleted_at IS NULL

... BUT ... it does not remove default_scope for folders that are used behind the curtain. Thus, we still have the condition folders.deleted_at IS NULL , which does not allow me to retrieve files belonging to those folders where deleted_at is not null.

I want to continue using act_as_paranoid, as it is incredibly useful in all other places of my application, and I try not to do something like manual filtering and drop-down elements of the .where_values array. But I'm not too good at handling complex areas or methods.

+7
source share
1 answer

Well, my question went down - they voted, not knowing why. But I found the answer:

When in has_many through, the problem I ran into was the inability to control the scope of the pass-through model (in this case the folders).

Turns out you can just do it

 @myvar = Folder.unscoped { current_user.files.with_deleted } 

Whoever submitted it, I would like to know why, so I can ask more interesting questions next time. Thanks!

+9
source

All Articles