RailsAdmin - Customizing your own filters
Asked Answered
A

1

8

I'm using https://github.com/sferik/rails_admin to handle my admin interface.

It's possible to filter your model based on the current columns that exists in this model (id, created_at etc.)

I want to be able to add custom filters for associations.

For example:

When I'm exploring the "Towns" model I want to be able to show only towns that have one or more projects.

I could do this by adding a new column to towns, called has_projects as a boolean that will be set to true when there are 1 or more projects associated, but I feel there must be a cleaner way to make your own custom filters ?

Attune answered 8/12, 2011 at 14:37 Comment(1)
See #26181443Orography
T
4

You can try using enum. See https://github.com/sferik/rails_admin/wiki/Enumeration

I used for belongs_to association, like following:

field :partner_id, :enum do
  enum do
    Partner.all.collect {|p| [p.name, p.id]}
  end
end

And in list view, added:

list do
  filters [:partner_id]
  ...
end    
Trevino answered 6/3, 2014 at 11:28 Comment(1)
This doesn't seem to apply to the question. You're giving possible values to filter/select for an existing database field, :partner_id. The OP is asking about filtering based on a query: "show me towns with 1 or more associated projects".Orography

© 2022 - 2024 — McMap. All rights reserved.