Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Hi I'm working on a Rails 3.2.9 app which uses devise and ActiveAdmin. I have models like users and company. I want admin to select one manager from list of registered users for a particular company.

My user's table has these fields : id,username, email, company_id, password etc

My company's table has : id, company_name, manager_id (this is nothing but id of user), created_at etc

After the admin has selected a manager for a company , i want his name(username from users table) to appear in the index page for company but now only manager_id is shown in index page. (On edit, the dropdown for selecting manager shows list of usernames)

Pls let me know how to use a query/joins statement to accomplish this

Here's my companies.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
like image 996
Aks.. Avatar asked Jun 18 '13 11:06

Aks..


1 Answers

Since you have used manager_id instead of user_id, active admin consider it as a single integer value.

To get fetch record automatic using id you can generate migration and change column as user_id.

If you dont want to change column name in then pass the if to User record as,

column "Manager" do |m|
  usr = User.find(m.manager_id)
  link_to usr.title, admin_user_path(m.manager_id)
end 
like image 69
bunty Avatar answered Oct 16 '22 08:10

bunty