Last Updated: September 09, 2019
·
2.046K
· codingfu

Active Admin: Propper relation names in filters

Imagine the following situation:

app/models/post.rb:

class Post < ActiveRecord::Base
  belongs_to :user
end

app/admin/post.rb:

ActiveAdmin.register Post do 
  filter :user
end

You will see a dropdown for user in filter sidebar with values like #User:0x007f89551ea448

If you want to set a propper name for associated model
just define a method "name" for it:

app/admin/user.rb:

class User < ActiveRecord::Base
  def name
    self.email
  end
end

And you'll see a propper email in a dropdown

1 Response
Add your response

I believe ActiveAdmin uses display_name as the method behind the scenes, but will go through a list of possible methods to call on the object in order, such as: name, title, display_name, & to_s

over 1 year ago ·