stooshie45
stooshie45

Reputation: 69

Why doesn't this has_many :through collection_check_boxes form input not create the records correctly?

I have a has_many :through association setup in a Rails 7 app.

I'm using the ActsAsBookable gem to handle bookings for events. An Event has_many EventExtras that can be added from the booking, and a booking has_many SelectedExtras. There's a tiny bit of added complexity because of the ActsAsBookable gem.

I'll cut down the code examples down to the relevant bits

module BookingMonkeyPatch
  extend ActiveSupport::Concern
  included do
    has_many :selected_extras, foreign_key: "acts_as_bookable_booking_id", dependent: :destroy
    has_many :event_extras, through: :selected_extras
    belongs_to :booking_set, optional: true
  end
end
ActsAsBookable::Booking.include(BookingMonkeyPatch)
class EventExtra < ApplicationRecord
  belongs_to :event
  has_many :selected_extras
  has_many :acts_as_bookable_bookings, through: :selected_extras, class_name: "ActsAsBookable::Booking", foreign_key: "acts_as_bookable_booking_id"
end
class SelectedExtra < ApplicationRecord
  belongs_to :event_extra
  belongs_to :acts_as_bookable_booking, foreign_key: "acts_as_bookable_booking_id", class_name: "ActsAsBookable::Booking"
end
class Event < ApplicationRecord
  has_many :event_extras, dependent: :destroy
end
class BookingSet < ApplicationRecord
  has_many :acts_as_bookable_bookings, class_name: "ActsAsBookable::Booking", foreign_key: "booking_set_id"
end

In my form, I am using the following:

<%= simple_form_for @booking_set, data: {turbo: "false"} do |form| %>

  <%= form.input :user_id, as: :hidden, input_html: { value: @user.id } %>
  <%= form.simple_fields_for :acts_as_bookable_bookings do |booking_subform| %>
    <%= render 'acts_as_bookable_booking_fields', f: booking_subform %>
  <% end %>
  <%= link_to_add_fields "Add Another Booking", form, :acts_as_bookable_bookings, "build_shoot" if @bookers&.size > 1 %>

<% end %>

and then inside the nested form, I have:

<%= f.collection_check_boxes :event_extra_ids, @selected_event.event_extras, :id, :name, {}, { :multiple => true } %>

This works fine. This form nests another 2 levels down, and all the nesting works fine. The issue I have is that when I try and submit the form with one of the extras selected, I get a validation error that essentially says in order to create the SelectedExtra it requires an acts_as_bookable_booking_id

This makes sense to me in some ways. Looking at the submitted params it's just event_extra_ids => {"24", "25"}. In order to create a SelectedExtra, it needs to also know what booking the selected extra is for. I thought the collection_select or collection_check_boxes would handle this automagically like other suggest but it doesn't seem to work.

Any ideas where I'm going wrong here?

Upvotes: 0

Views: 46

Answers (1)

Allison
Allison

Reputation: 2343

It looks like accepts_nested_attributes_for isn't defined on your model... see RoR Guide: Building Complex Forms.

If you're having issues beyond that, I'd comment out <%= render 'acts_as_bookable_booking_fields', f: booking_subform %> and temporarily replace it with whatever's in 'acts_as_bookable_booking_fields' to more easily troubleshoot whether it's an issue with the form structure or the nesting.

Upvotes: 0

Related Questions