Rails 3 and ActiveAdmin. Filter displays objects not company name

I have a list of clients, but in the filter section in the right column I get a list similar to this #<Customer0X0B500> in the selection menu. How can I display the company_name attribute of a client instead?

+6
source share
4 answers

It turned out, thanks!

filter :customer, :collection => proc {(Customer.all).map{|c| [c.company_name, c.id]}}

+17
source

I'm not sure I understand you, but you should probably define the to_s method inside your Customer class, for example.

 class Customer def to_s self.company_name end end 

it would be easier if you shared some code

+4
source
 class Customer def display_name self.company_name end end 

Defining the name display_name instead of to_s works better ...

  • to_s can be called automatically by other objects.
  • display_name only affects ActiveAdmin

You can also define:

 show :title => :display_name 

This will cause your company name to appear as the name on the watch pages, not Company #x.

+2
source

So that the ActiveAdmin display selection menu is used correctly in Model.rb: -

alias_attribute: name ,: category_name

+1
source

Source: https://habr.com/ru/post/905214/


All Articles