活动管理范围和过滤器不起作用
Active admin- scope and filter not working
我在 rails 4.2 应用程序中使用 active-admin(AA, 1.0.0)。我正在显示在线用户列表。我想添加 'scope' 以便不同类型用户的链接及其计数显示在主列表上方。
ActiveAdmin.register User do
menu parent: "Users", label: "Online Users", url: '/ admin/users/online_users'
collection_action :online_users, method: :get do
@users = User.select{|i| i.online?}
end
belongs_to :organization, optional: true
scope :all, default: true
scope :admins do |users| users.with_role(:admin) end
scope :type1 do |users| users.with_role(:type1) end
scope :type2 do |users| users.with_role(:type2) end
end
显示了列表,但没有显示范围。我错过了什么?
与其使用 collection_action
来获取您想要的子集,不如使用 scoped_collection
将您的注意力限制在在线用户身上。这样,其他一切都可以正常工作。
理想情况下,您的 users
table 将有一个 online
布尔列,在这种情况下添加一个简单的 where
子句就可以了。如果不是,即online?
是一个计算方法,无法查询,那么你需要先计算在线用户id
的集合。这不会很好地扩展,所以要小心。
ActiveAdmin.register User do
menu parent: "Users", label: "Online Users", url: '/admin/users/online_users'
controller do
def scoped_collection
# if online is a boolean column (best performance):
super.where(online: true)
# if online is a computed method (convert to ActiveRecord::Relation):
# ids = User.all.select(&:online?).map(&:id)
# super.where(id: ids)
end
end
belongs_to :organization, optional: true
scope :all, default: true
scope :admins, -> (u) { u.with_role(:admin) }
scope :type1, -> (u) { u.with_role(:type1) }
scope :type2, -> (u) { u.with_role(:type2) }
end
您也可以在您的普通 users
管理路由(假设您有一个)上使用过滤器来实现此目的。
我在 rails 4.2 应用程序中使用 active-admin(AA, 1.0.0)。我正在显示在线用户列表。我想添加 'scope' 以便不同类型用户的链接及其计数显示在主列表上方。
ActiveAdmin.register User do
menu parent: "Users", label: "Online Users", url: '/ admin/users/online_users'
collection_action :online_users, method: :get do
@users = User.select{|i| i.online?}
end
belongs_to :organization, optional: true
scope :all, default: true
scope :admins do |users| users.with_role(:admin) end
scope :type1 do |users| users.with_role(:type1) end
scope :type2 do |users| users.with_role(:type2) end
end
显示了列表,但没有显示范围。我错过了什么?
与其使用 collection_action
来获取您想要的子集,不如使用 scoped_collection
将您的注意力限制在在线用户身上。这样,其他一切都可以正常工作。
理想情况下,您的 users
table 将有一个 online
布尔列,在这种情况下添加一个简单的 where
子句就可以了。如果不是,即online?
是一个计算方法,无法查询,那么你需要先计算在线用户id
的集合。这不会很好地扩展,所以要小心。
ActiveAdmin.register User do
menu parent: "Users", label: "Online Users", url: '/admin/users/online_users'
controller do
def scoped_collection
# if online is a boolean column (best performance):
super.where(online: true)
# if online is a computed method (convert to ActiveRecord::Relation):
# ids = User.all.select(&:online?).map(&:id)
# super.where(id: ids)
end
end
belongs_to :organization, optional: true
scope :all, default: true
scope :admins, -> (u) { u.with_role(:admin) }
scope :type1, -> (u) { u.with_role(:type1) }
scope :type2, -> (u) { u.with_role(:type2) }
end
您也可以在您的普通 users
管理路由(假设您有一个)上使用过滤器来实现此目的。