1

when using rails_admin for associated objects (like has_and_belongs_to) it shows the ID of the object as the association. This isn't a great deal for the users so I'ld like to change this for showing the text of the associated object.

Is this solvable?

Here a little example:

First Model:

class Menu
  include Mongoid::Document

  field :date, type: Date

  has_and_belongs_to_many :meal
end

Second Model:

class Meal
  include Mongoid::Document

  field :text, type: String

  has_and_belongs_to_many :menu
end

So it shows something like this:

So it shows something like this:

But I'ld love to see the Text of the meals instead.

PascalTurbo
  • 2,189
  • 3
  • 24
  • 41

2 Answers2

6

Simply define a title-method do the trick:

def title
  self.text
end
PascalTurbo
  • 2,189
  • 3
  • 24
  • 41
  • Hi sir, can you check my forum? I have question similar to this. https://stackoverflow.com/questions/44234531/how-can-i-display-the-name-instead-of-an-id-without-the-name-column/44234592?noredirect=1#comment75480115_44234592 – Angel May 29 '17 at 06:54
2

You could use the RailsAdmin DSL object_label_method to change how the field is presented to the user.

In your case, something like this might do the trick:

RailsAdmin.config do |config|
  config.model Menu do
    list do
      field :meal do
        pretty_value do
          value.text
        end
      end
    end
  end
end
JeanMertz
  • 2,250
  • 2
  • 21
  • 26