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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With