Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails acts_as_paranoid and has_many :through

So I'm using the rails3_acts_as_paranoid gem, and having some problems controlling scope 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 lets just say somewhere in the users_controller.rb i want to query all files belonging to a user, whether they are deleted, and/or belong to folders that have been deleted. So naturally I would assume to do something like the following

current_user.files.with_deleted

with_deleted method does it's job at removing the files.deleted_at IS NULL

...BUT... it doesnt remove the default_scope for folders which is used kind of behind the curtain. So we still have a folders.deleted_at IS NULL condition, preventing me from retrieving the files that belong to those folders where deleted_at is not null.

I want to keep using acts_as_paranoid, as it is incredibly useful in all other places of my app, and am trying not to do something like manual filtering and popping out elements of the .where_values array. But I don't know too much about handling complex scopes or what methods are available.

like image 799
Misterparker Avatar asked Jan 07 '13 01:01

Misterparker


1 Answers

Well my question got down-voted, not sure why. But I found the answer:

When on a has_many through, the problem I was having was an inability to control the scope of the through model (Folders in this case).

Turns out you can just do this

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

To whoever downvoted it - I'd like to know why, so I can ask better questions next time. Thanks!

like image 121
Misterparker Avatar answered Sep 21 '22 09:09

Misterparker