0

I have an app which registers applicants as Participants and then assigns them to Groups. The two models are associated via has_and_belongs_to_many relationship. There are other hanging models related to Participant, but they are not connected to Groups.

I'd like to be able to assign a Participant to a Group when I create a new Group in active admin.

My two models are joined through a join table called matchups.

My Group model schema is as follows:

    create_table "groups", force: :cascade do |t|
t.string   "description"
t.integer  "participant_id"
t.datetime "created_at",     null: false
t.datetime "updated_at",     null: false
t.index ["participant_id"], name: "index_groups_on_participant_id"
   end

My Participant model schema is as follows:

    create_table "participants", force: :cascade do |t|
t.string   "first_name"
t.string   "last_name"
t.date     "birthdate"
t.string   "email"
t.string   "phone"
t.string   "street_name"
t.string   "city"
t.string   "state"
t.string   "zip"
t.string   "role"
t.datetime "created_at",  null: false
t.datetime "updated_at",  null: false
t.string   "gender"
   end 

My active admin resources allow the following params: For Groups:

    permit_params :id, :description, :participant_id, :student_detail_id, :volunteer_detail_id

For Participants:

    permit_params :id, :first_name, :last_name, :gender, :email, :birthdate, :phone, :street_name, :city, :state, :zip, :role

When I got to create a new Group in active admin, the only field I am able to fill out is :description.

I'd like to assign the new group to one or more :participant_id.

dbate
  • 127
  • 13

2 Answers2

2

write a custom form in admin/groups.rb to accept multiple participant_ids like below

ActiveAdmin.register Group do
permit_params :description , participant_ids: []
  form do |f|
    f.inputs 'Group Details' do
      f.input :description
      f.input :participant_ids, as: :check_boxes, collection: Participant.all
     end
  end
end
uday
  • 1,421
  • 10
  • 19
  • Thanks! This worked, but I have a follow-up question: if I want the participants to show up using their first_name attribute, is this possible? I assume so but I have tried :participant_ids.first_name and Participant.first_name with no luck. – dbate Jan 30 '19 at 18:10
  • 1
    Yes, you can do that. Replace second input line with this `f.input :participant_ids, as: :check_boxes, collection: Participant.pluck(:first_name, :id)` – uday Jan 31 '19 at 05:30
  • That worked! Is it possible to put multiple attributes? When I use this solution, the :id attribute no longer shows up. – dbate Jan 31 '19 at 14:55
  • :id will be in hidden field and what do you mean by multiple attributes? – uday Jan 31 '19 at 14:58
  • For example, if I'd like to add the :last_name or :gender as well, would that be possible? I've tried several variations but no luck – dbate Jan 31 '19 at 15:43
  • Display only, just for clarification in the event there are several participants with the same first and/or last names- I found the gem pluck_all which does the trick but doesn't look very clean. Do you have any other suggestion? – dbate Feb 01 '19 at 04:19
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/187697/discussion-between-uday-and-dbate). – uday Feb 01 '19 at 04:25
0

I don't have enough reputation to comment on your post, so I'll ask my question as an answer and edit it later.

Can you check your server logs and see what data is being sent to the create action? If I had to guess, the participant_id is never being sent.

Noah
  • 550
  • 2
  • 8