How to display name instead of id using active admin in rails 3

Hi, I am working on a Rails 3.2.9 application that uses devise and ActiveAdmin. I have models like users and companies. I want the administrator to choose one manager from the list of registered users for a particular company.

My user table has the following fields: id, username, email, company_id, password, etc.

My company’s table contains: id, company_name, manager_id (this is nothing more than a user ID), created_at, etc.

After the administrator has selected a manager for the company, I want his name (username from the user table) to appear on the index page for the company, but now only the manager_id is displayed on the index page. (When editing, the drop-down list for manager selection shows a list of usernames)

Pls tell me how to use query / join to accomplish this

Here is my company.rb file for admin

ActiveAdmin.register Company do index do column "Company", :name column :address column "No. of Subscriptions", :no_of_licenses column "Manager", :manager_id default_actions end filter :name form do |f| f.inputs "Company Details" do f.input :name f.input :address f.input :no_of_licenses, :label => 'No of Subscriptions' f.input :manager_id, :as => :select, :collection => User.joins('LEFT OUTER JOIN companies ON users.company_id = companies.id').map{|u| [u.username, u.id]} end f.buttons end end 
+4
source share
2 answers

As @Bunty said, we have to pass the user record to the manager column. The following code works!

  column "Manager" do |m| usr = User.find(m.manager_id).username end 

Now the name of the manager is displayed, not manager_id, as before

+2
source

Since you used manager_id instead of user_id, the active administrator treats it as a single integer value.

To get an automatic record of a record using an identifier, you can create a migration and change the column as user_id.

If you do not want to change the name of the column, then pass the if record to User, as

 column "Manager" do |m| usr = User.find(m.manager_id) link_to usr.title, admin_user_path(m.manager_id) end 
+9
source

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


All Articles