【发布时间】:2015-06-17 12:15:08
【问题描述】:
我有一个 ActiveAdmin 实例,其中我有 User 和 has_many Things。我还希望通过使用Employments 模型与其他User 关联,使用自引用has_many 允许用户充当其他User 的Things 的经理。模型看起来像这样......
class User < ActiveRecord::Base
...
has_many :things, dependent: :destroy
has_many :employments, foreign_key: 'employer_id', dependent: :destroy
has_many :employees, through: :employments
has_many :inverse_employments, class_name: 'Employment', foreign_key:
'employee_id', dependent: :destroy
has_many :employers, through: :inverse_employments
accepts_nested_attributes_for :employments, allow_destroy: true
...
end
class Employments < ActiveRecord::Base
belongs_to :employer, :class_name => 'User'
belongs_to :employee, :class_name => 'User'
# Also has other metadata attributes
end
class Thing < ActiveRecord::Base
belongs_to :user
end
在 ActiveAdmin 中,我希望用户能够管理自己的事物以及其他用户的事物。我已经设置了范围和过滤器,如下所示
ActiveAdmin.register Thing do
...
scope_to :current_user
scope 'Owned', :all, default: true do
Rezzable::Pinger.where(user_id: current_user.id)
end
scope :managed do
Rezzable::Pinger.where(user_id: Employment.where(employee_id:
current_user.id).collect{ |e| e.employer_id })
end
end
范围界定效果很好,但过滤器不起作用。如果我删除范围,过滤器工作正常。我还致力于创建一个只显示管理或拥有的物品的过滤器,但事实证明,洗劫协会也很困难,因为拥有物品的协会是直接的,但另一个必须通过就业和获取雇主的物品来完成。
有没有办法让范围和过滤器很好地协同工作?如果做不到这一点,是否有替代的,也许是更好的方法来实现这一点?提前致谢。
PS - 我确实认为这与 ActiveAdmin - Using scopes with filters 存在相同的问题,但我认为这种解决方案对我不起作用,因为我们的关联非常不同。
【问题讨论】:
标签: ruby-on-rails filter scope activeadmin