It seems ideal to me, that each Meeting Entry would store the
Tournament_id and attendence for each Member_id. However, if you were
to do this, you would need a dynamic column length for the member_ids
(one column for each member), which can vary.
Big nope. That's not a tenable solution for modeling data in a relational database as the schema would be completely unmaintable and you would end up with a huge amount of columns that almost entirely contain nulls. The SQL queries would also be very crazy.
You instead need to think in terms of each row in a table containing the unique data for each member. In database design this corresponds to the concept First Normal Form.
This is a simplefied example of how you would keep track of attendence*:
class Member
has_many :attendences
has_many :meetings, through: :attendences
end
class Meeting
has_many :attendences
has_many :members, through: :attendences
end
# rails g model attendence member:belongs_to meeting:belong
class Attendence < ApplicationRecord
belongs_to :member
belongs_to :meeting
end
Here attendences
is a join table containing the member and meeting ids and we have a many to many assocation between Member and Meeting.
You can then just use the rails form helpers for creating checkboxes from collections together with the member_ids
setter / getter created by has_many :members, through: :attendences
:
<%= form_with(model: @meeting) do |form| %>
<div class="field">
<%= form.label :member_ids, 'Members in attendence' %>
<%= form.collection_checkboxes :member_ids, @available_members, :id, :name %>
</div>
# ...
<% end %>
In your controller you would then whitelist the member_ids
column with a hash key with an empty array as the value:
class MeetingsController < ApplicationController
def new
@meeting = Meeting.new
@available_members = Member.all
end
def create
@meeting = Meeting.new(meeting_params)
if @meeting.save
redirect_to @meeting
else
@available_members = Member.all
render :new
end
end
private
def meeting_params
params.require(:meeting)
.permit(:foo, :bar, :baz, member_ids: [])
end
end
This will permit an array of permitted scalar values such as a list of ids.