'Join Table ActiveRecord Invalid Record on Save (Rails 5)

I have a join table between freebies and subcategories that is acting up.

(I have an existing join table between freebies and categories that is absolutely fine, after which the subcategories is modeled, but I can't tell where the difference is that makes it act up.)

Here's my freebie.rb model:

class Freebie < ApplicationRecord
  has_many :blogs
  has_one_attached :image
  has_one_attached :sample_image
  has_one_attached :t1_image
  has_one_attached :fb_share_image
  has_many_attached :pin_image

  extend FriendlyId
  friendly_id :name, use: :slugged

  has_many :categorization_freebies
  has_many :categories, through: :categorization_freebies

  has_many :subcategorization_freebies
  has_many :subcategories, through: :subcategorization_freebies
end

And my subcategory.rb model:

class Subcategory < ApplicationRecord
  belongs_to :category

  has_many :subcategorization_freebies
  has_many :freebies, through: :subcategorization_freebies
end

And finally the subcategorization_freebie.rb join table:

class SubcategorizationFreebie < ApplicationRecord
  belongs_to :subcategory
  belongs_to :freebie
end

So here's the form that's used to create the new freebie (where the error is appearing):

<%= simple_form_for(@freebie) do |f| %>
  ...
      <div class="form-group form-check jsItUp">
      <h5>Categories</h5>
      <%= f.collection_check_boxes(:category_ids, Category.all, :id, :name ) %>
    </div>

    <div class="form-group form-check jsItUp">
      <h5>Subcategories</h5>
      <%= f.collection_check_boxes(:subcategory_ids, Subcategory.all, :id, :name ) %>
    </div>
 ...
<% end %>

The permissions in the freebies_controller.rb controller are like this:

def freebie_params
  params.require(:freebie).permit(
    :name,
    :category,
    :short_description,
    :convertkit_url,
    :active,
    :thrivelink,
    
    :image,
    :sample_image,
    :t1_image,
    :fb_share_image,
    :pin_image,

    :top_question,
    :main_heading,
    :main_subheading,
    :t1_short,
    :t1_name,
    :t1_title,
    :t1_long,
    :wyg1,
    :wyg2,
    :wyg3,
    :outcome,
    :outcome_2,

    :ml_id,
    :ml_submit_code,
    :after_download_url,
    :ml_img_track_url,

    category_ids: [],
    subcategory_ids: []
  )
end

When the form is submitted it gives an error that says Validation failed: Subcategories is invalid.

The server log when this occurs goes like this:

app/controllers/freebies_controller.rb:33
   (0.2ms)  ROLLBACK
  ↳ app/controllers/freebies_controller.rb:33
Completed 422 Unprocessable Entity in 15ms (ActiveRecord: 5.3ms)


ActiveRecord::RecordInvalid - Validation failed: Subcategories is invalid:
  app/controllers/freebies_controller.rb:33:in `block in create'
  app/controllers/freebies_controller.rb:32:in `create'

Can anyone see where I'm going wrong? Most of the code was copied directly from the categories relationship...which works just fine. Help!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source