ruby-on-railsrubyassociationsrails-admin

How to override value that appears in a dropdown in the rails_admin gem


I have a model UserDelegation that contains:

belongs_to :delegator, :class_name => 'User'#, :conditions => {:order => 'users.last_name ASC, users.first_name ASC'}
belongs_to :delegatee, :class_name => 'User', :touch => true#, :conditions => {:order => 'users.last_name ASC, users.first_name ASC'}

and in my user model i have this:

has_many :delegatees, :through => :user_delegatees, :order => 'users.last_name ASC, first_name ASC'
has_many :delegators, :through => :user_delegators, :order => 'last_name ASC, first_name ASC'

when i am in rails admin and go to the User delegation table and add a new one i get two drop downs which lists the users name. I do know that rails_admin uses name by default when it populates drop downs in associations.

What i am wanting to know is how to override what is displayed in these drop downs so I could for example have it display 'user's name - user's location'?


Solution

  • Change the object_label_method of your User model for rails_admin

    class User < ActiveRecord::Base
    
      ...
    
      rails_admin do
        object_label_method :name_location
      end
    
      def name_location
        "#{name} - #{location}"
      end
    end