Search code examples
ruby-on-railsrails-activerecordactiveadminhas-and-belongs-to-many

How do I get associated records to show in active admin?


I have a registration app which records a Participant model, which has two hanging models- Student_Detail and Volunteer_Detail. Participant has_one of Student_Detail and Volunteer_Detail.

There is a third model, Group. Group records which participants are matched to each other. Because Participant may be involved in more than one Group and a Group may have more than one Participant, I assume has_and_belongs_to_many is the best association for Participant and Group.

A join table exists for Group and Participant

I am newer to Rails and I am using active admin to manage this app.

When I login, I can see each model that I've generated as a resource, but I am hoping to be able to show associated details (Student/Volunteer/Group)of each participant when I view the Participant tab. Currently, it only shows attributes which are only for that specific model, not for any associated models.

Here is my active_admin resource code for the model Participant:

    ActiveAdmin.register Participant do

                            permit_params :first_name, :last_name, :gender, :email, :birthdate, :phone, 
                                              :street_name, :city, :state, :zip, :role, student_details_attributes: [:nationality, :religion, :need_ride, 
                                            :has_spouse, :spouse_name, :english_level, :expectation, :length_of_stay, :exact_length, :volunteer_id, 
                                            :matched, :returned_home, :participant_id], volunteer_details_attributes: [:date, :importance, :story, :questions, :participant_id]


                            end

I'd like to be able to view all attributes associated with an entry in the Participant table when I view it in Active Admin. Currently I only see attributes that are directly from that model (:first_name, :last_name, :gender, :email, :birthdate, :phone, :street_name, :city, :state, :zip, :role)


Solution

  • Let me try to guess, you need to display attributes of associations

    show do |participant|
      attributes_table do
        row :id
        row :full_name
        attributes_table_for participant.volunteer_detail do
          row :id
          row :volunteer_name
        end
      end
    end