I have this:
<%= f.collection_select :category_ids, Category.all, :id, :name, {} %>
inside my form and it creates the record no problem, however I'm lost on how to make more two or more records inside the same form, all the things I try either just create the one record or none at all. I found this solution, which I analyzed and was sure was gonna work (altough I don't really get on why it uses nil):
<%= f.fields_for :category_ids do |category| %>
<%= category.collection_select(nil, Category.all, :id, :name,
{include_blank: "---", selected: 0},
{id: :event_category_id_1}) %>
<%= category.collection_select(nil, Category.all, :id, :name,
{include_blank: "---", selected: 0},
{id: :event_category_id_2}) %>
<% end %>
but this time it creates no record at all.
Here are my models:
class Event < ApplicationRecord
has_many :categorizations
has_many :categories, through: :categorizations
accepts_nested_attributes_for :categorizations
end
class Category < ApplicationRecord
has_many :categorizations
has_many :events, through: :categorizations
end
class Categorization < ApplicationRecord
belongs_to :event
belongs_to :category
end
You want to assign more than one
Category
to yourModel
? Post your model so we can figure what's going wrong when storing it.Adding
multiple: true
(andsize: 5
) will expand your select field and by holding ctrl/cmd you can select multiple entries.