I have an ActiveAdmin instance where I have User
that has_many
Thing
s. I also want to allow users to act as managers for other User
's Thing
s using a self referential has_many through association to Other User
using an Employments
model. The models look something like this...
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
In ActiveAdmin, I'd like a user to be able to manage their own Things and also other Users' things. I have set it up with scopes and filter as below
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
The scoping works great, but then the filters don't work. If I remove the scopes the filters work fine. I've also worked at creating a filter that shows only managed or owned items, but the ransacking the association is proving difficult as well since the association for owned Things is direct, but the other must be done through Employments and getting the employer's Things.
Is there a way to get scopes and filters to work nicely together? Failing that, are there alternative, perhaps better ways to accomplish this? Thanks in advance.
PS - I do see this as the same problem as ActiveAdmin - Using scopes with filters, but I don't think this solution works for me because of our very different associations.