如何覆盖在rails_admin gem中的下拉列表中显示的值

问题描述:

我有一个包含以下内容的模型UserDelegation:

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'

当我在rails admin中并转到用户委托"表并添加一个新的代理时,我会看到两个下拉列表,其中列出了用户名.我确实知道rails_admin在填充关联下拉列表时会默认使用name.

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'?

User模型的object_label_method更改为rails_admin

class User < ActiveRecord::Base

  ...

  rails_admin do
    object_label_method :name_location
  end

  def name_location
    self.name + ' - ' + self.location
  end
end