Which is the best use of discard (or act_as_paranoid) in rails-admin?

541 Views Asked by At

I'm using the gem discard and also rails-admin. I would like to know which is the best approach to list my active users in a shop in rails admin, taking into consideration the users kept

I've created a method in the model shop:

class Shop < ApplicationRecord
  include ShopRailsAdmin

 has_many :users

 def active_users
  users.kept
 end
end

In rails admin I'm using:

field :active_users do
  label 'Users'
end

But I'm receiving an AssociationRelation instead of a CollectionProxy so in the view, the association looks like

#<User::ActiveRecord_AssociationRelation:0x00007f9c34c1f8e0>

Is there another way to do this so I can avoid defining the method in the model shop?

PD: the tag should have been also discard but it does not exist and I could not create it.

Thanks!

1

There are 1 best solutions below

0
Guillermo Siliceo Trueba On

You need to define it as an scoped association

class Shop < ApplicationRecord
  include ShopRailsAdmin

 has_many :users
 has_many :active_users, -> lambda { 
    where(discarded_at: nil) 
 }, class_name: 'User'
end

I'm assuming you did not personalize the discard_column.

Rails admin should display them right.