Search code examples
ruby-on-railsrubyexportrails-admin

Rails admin - customize difference values of an association in Export section


I have two models . JobPosition and LineManager . A line manager can have different job positions and a job position has a line manager . In rails admin I want to export different columns which are representing the line manager attributes including: name , department and etc!

config.model 'JobPosition' do
  export do
    field :line_manager, :string do
      export_value do
        value.name if value #value is an instance of Teacher
      end
    end
    field :line_manager, :string do
      export_value do
        value.title if value #value is an instance of Teacher
      end
    end
    field :line_manager, :string do
      export_value do
        value.department if value #value is an instance of Teacher
      end
    end
    field :line_manager, :string do
      export_value do
        value.description if value #value is an instance of Teacher
      end
    end
    field :line_manager, :string do
      export_value do
        value.job_level if value #value is an instance of Teacher
      end
    end
  end
end

but it just apply the first one not The others . I also want to change the column header .I tried to add labels like this :

field :line_manager, :string do
      export_value do
        lable "line manager name"
        value.name if value #value is an instance of Teacher
      end
end

but it is not correct I suppose. Do you have any idea ?


Solution

  • One possible solution is to export virtual fields like this example

    field :line_manager_departmant, :string do
      label "Line Manager Department"
      pretty_value do
        bindings[:object].line_manager.department
      end
    end
    
    field :line_manager_description, :string do
      label "Line Manager Description"
      pretty_value do
        bindings[:object].line_manager.description
      end
    end