Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overide Default_scope #161

Open
jeremylynch opened this issue Jul 13, 2014 · 4 comments
Open

Overide Default_scope #161

jeremylynch opened this issue Jul 13, 2014 · 4 comments
Labels
Milestone

Comments

@jeremylynch
Copy link

I have found that @activity.trackable does not work when there is a default_scope which excludes the tracked object. I found a working monkey patch (see below), though is there a more appropriate solution? Would it be possible to override this default_scope when it's present?

PublicActivity::Activity.class_eval do
  def trackable
    trackable_type.constantize.unscoped.find trackable_id
  rescue
    nil
  end
end
@pokonski
Copy link
Member

That is a very good point. I'll include that in the new version!

@pokonski pokonski added the bug label Jul 13, 2014
@pokonski pokonski added this to the 2.0 milestone Jul 13, 2014
@selimekizoglu
Copy link

This is what I'm doing:

class Activity < PublicActivity::Activity
    def trackable
        model = Object.const_get self.trackable_type
        model.unscoped { super }
    end
end

@codenem
Copy link

codenem commented Mar 22, 2018

To also make it work if you need including ParentModel.includes(activities: :owner), you can override the association in an initializer (for activerecord apps; and e.g., inorder to unscope deleted_at):

PublicActivity::Activity.class_eval do
  belongs_to :owner, -> { unscope(where: :deleted_at) }, polymorphic: true
end

@pokonski
Copy link
Member

@Nemile's solution looks easy enough so I'll leave the codebase as-is and move it to a wiki page

@pokonski pokonski added docs and removed bug labels Jul 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants