I got some problems with saving records from a has_many :through
association. I must have missed something important.
First things first:
- Rails version: 5.1.4
- Ruby version: 2.4.2
I got these three models:
class Event < ApplicationRecord
has_many :events_timeslots
has_many :timeslots, through: :events_timeslots
end
class Timeslot < ApplicationRecord
has_many :events_timeslots
has_many :events, through: :events_timeslots
end
class EventsTimeslot < ApplicationRecord
belongs_to :event
belongs_to :timeslot
end
According to this, every event has many timeslots and every timeslot has many events.
I want a multi select in my view:
<%= form_with(model: event, local: true) do |form| %>
...
<% fields_for :events_timeslots do |events_timeslots| %>
<%= events_timeslots.label :timeslots %>
<%= events_timeslots.select(:timeslots, @timeslots.collect {|t| [t.name, t.id]}, {}, {multiple: true}) %>
<% end %>
...
<% end %>
Is this the right approach to select multiple timeslots while creating a new event? The timeslots already exist at this time but when the event gets saved, it should also create the associated records in the events_timeslots
table.
I also permit the timeslots
attribute in strong parameters:
params.require(:event).permit(:date, timeslots: [])
Is there a magic Rails-Way to use the "scaffolded" controller actions to create the new event as well as the associated records in EventsTimeslot model? Related to this question I found an answer on another question, however I wasn't able to get it to work!
Maybe I missed a very stupid little thing, but anyways thanks for the help.
Edit
The (messed up) events_timeslots
table in schema.rb
:
create_table "events_timeslots", force: :cascade do |t|
t.bigint "events_id"
t.bigint "timeslots_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["events_id"], name: "index_events_timeslots_on_events_id"
t.index ["timeslots_id"], name: "index_events_timeslots_on_timeslots_id"
end