简体   繁体   中英

How to provide collection to Activeadmin nested resource filter?

How to provide as collection users that are associated with group?

has_many :users, through: :group_settings
has_many :groups, through: :group_settings

ActiveAdmin.register Group do
  ActiveAdmin.register GroupSetting do
    belongs_to :group
    filter :user_email, as: :select, collection: Users.????, label: 'Email'
  end
end

Thanks!

Solution for case when your user may belong to multiple groups:

In your model:

has_and_belongs_to_many :groups

scope :with_group_id, ->(group_id) { joins(:groups).where(groups: {id: [*group_id] } ) }

and usage:

User.with_group_id([2,5])

where 2 and 5 - group ids

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM