Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Admin filter on two-level deep belongs_to association

Say a Person has many House and House has many Door. (So Door has a house_id and House has a person_id field). I have a list of Doors in Active Admin, in which I want a filter that allows me to choose a person, and show me all doors in all of that person's houses.

ActiveAdmin.register Door do
  filter :knob_color
  filter :is_open
  filter :has_window

  filter :house_person      # DOESN'T WORK
  filter :house_person_name # DOESN'T WORK

The Person model table has a name field, so ActiveAdmin should be able to pick that up.

How do I do this?

UPDATE

As requested, my situation rephrased in models.

# id:   integer
# name: string
class Person < ActiveRecord::Base
  has_many :houses
end

# id:        integer
# person_id: integer
class House < ActiveRecord::Base
  belongs_to :person
  has_many   :doors
end

# id:         integer
# house_id:   integer
# knob_color: string
# is_open:    boolean
# has_window: boolean
class Door < ActiveRecord::Base
  belongs_to :house
end
like image 509
Pelle Avatar asked Dec 25 '22 14:12

Pelle


1 Answers

You need to register a person association on Door using a has_one relation through house, and then you can add the filter directly.

class Door < ActiveRecord::Base
  belongs_to :house
  has_one :person, through: :house
end

ActiveAdmin.register Door do
  filter :person
end
like image 184
ahmacleod Avatar answered Apr 05 '23 23:04

ahmacleod