我有一个经典的帖子属于用户关联。我想对所有操作(如#index
)应用default_scope,以便它仅列出我的帖子。但是我希望能够在我转到#show
操作时看到任何人的帖子,如果点击它的链接。
如何避免要应用于该操作的default_scope?
class Post < ActiveRecord::Base
belongs_to :user
end
ActiveAdmin.register CertificationModel do
controller do
def scoped_collection
current_user.posts
end
end
end
您可以为不希望调用scoped_collection
的其他操作调用 super。
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
解决方案很简单:保持scoped_collection
并重新定义#show
行动。
ActiveAdmin.register Post do
controller do
def show
@post = Post.find params[:id]
end
def scoped_collection
current_user.posts
end
end
end