I have a classic posts-belongs-to-user association. I want to apply a default_scope to all the actions like #index
so it lists my posts only. But I want the ability to see posts from anybody when I go to the #show
action if follow a link to it.
How can I avoid the default_scope to be applied on that action?
class Post < ActiveRecord::Base
belongs_to :user
end
ActiveAdmin.register CertificationModel do
controller do
def scoped_collection
current_user.posts
end
end
end
The solution was simple: to keep scoped_collection
and to redefine #show
action.
ActiveAdmin.register Post do
controller do
def show
@post = Post.find params[:id]
end
def scoped_collection
current_user.posts
end
end
end
You can call super for other action on which you don't want your scoped_collection
to be called.
ActiveAdmin.register Post do
controller do
def scoped_collection
if ['index', 'action2', 'action3'].include?(params[:action])
current_user.posts
else
super
end
end
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With