10

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.

Misterparker
  • 596
  • 5
  • 16
  • It seems the #unscoped method is what you're looking for here : http://apidock.com/rails/ActiveRecord/Scoping/Default/ClassMethods/unscoped – pjam Jan 07 '13 at 01:28
  • I tried the unscoped method, but that drops the scope of automagic join bridging user to files, specifically `INNER JOIN folders ON files.folder_id = folders.id WHERE folders.user_id = 1` unless perhaps I missed a feature of unscoped to keep that? – Misterparker Jan 07 '13 at 01:34
  • I'm still checking if I find a solution, but just note that the line `@user = User.find current_user.id` is useless, as you're reloading the current_user – pjam Jan 07 '13 at 01:36
  • Ok updated it to be a little less useless :) – Misterparker Jan 07 '13 at 01:40
  • nice answer here : http://stackoverflow.com/questions/1540645/how-to-disable-default-scope-for-a-belongs-to – voondo Dec 13 '13 at 11:05

1 Answers1

12

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!

Misterparker
  • 596
  • 5
  • 16