In my Rails 6.1 app, I work on laboratories and journals.
A laboratory can edit many journals. For each journal, it can have a editor type (at least one). For instance, the laboratory LabA edits the journal JournalX as a “writer”. But it could also be a “checker” for the same JournalX.
A single journal can be edited by many laboratories. For instance, the same journal JournalX is also edited by the laboratory LabB as a “checker” (it could have been a writer, too, it doesn’t matter).
The journals table has two columns: titre : a title (string) issn : the issn number for this title (string too)
The laboratories (its name is in French : laboratoires) has many columns, of which, for instance, its name.
I have an type_editeurs table (poor choice of name in French, English editor_types would have been better) with the list of the possible editor types.
Finally a editions table joins between all these elements, containing :
- a laboratory id
- a journal id
- an editor type id
Then I have the following models:
class Journal < ApplicationRecord
has_many :editions, dependent: :destroy
has_many :laboratoires, through: :editions
has_many :type_editeurs, through: :editions
validates :issn, presence: true, length: {maximum: 10}, uniqueness: true
validates :titre, presence: true, length: {maximum: 256}
end
class TypeEditeur < ApplicationRecord
has_many :editions, dependent: :destroy
has_many :laboratoires, through: :editions
has_many :journals, through: :editions
validates :code, presence: true, length: {maximum: 10}, uniqueness: true
validates :libelle, presence: true, length: {maximum: 256}
end
class Edition < ApplicationRecord
belongs_to :laboratoire
belongs_to :journal
belongs_to :type_editeur
end
And finally :
class Laboratoire < ApplicationRecord
...
has_many :editions
accepts_nested_attributes_for :editions
has_many :journals, through: :editions
accepts_nested_attributes_for :journals
has_many :type_editeurs, through: :editions
accepts_nested_attributes_for :type_editeurs
...
end
In all the app, I am working through the laboratory.
In the laboratories’ controller, I have permitted :
editions_attributes [:id, :journal_id, :type_editeur_id]
And, since I was also a bit lost and tried many things, at the moment journal_ids: []
and type_editeur_ids: []
are also permitted.
In the view, I would like the list of all the journals to appear. On each line of the list, a checkbox next to the journal’s title allows the user to associate the journal to the laboratory. Moreover, on the same line, a select box allows the user to choose the editor_type for the chosen journal.
To that purpose, I have tried many ways to write my form but, sadly, until now, I’ve never managed to make it work successfully.
I managed to record/update the journals list, for instance, but the editor type is not working well.
For instance, I have the following code (form
is the general form to handle a laboratory creation/modification) :
...
<table class="table table-hover table-striped" style="width: 100%" id="tabjournal">
<thead>
<th scope="col">Titre</th>
<th scope="col">ISSN</th>
<th scope="col">Rôle</th>
</thead>
<tbody>
<%= form.fields_for :editions do |ff| %>
<%= ff.collection_check_boxes :journal_id, Journal.all.order(:titre), :id, :titre do |j| %>
<tr>
<td align="center" width="5%">
<%= j.check_box %>
</td>
<td>
<%= j.label %>
</td>
<td>
<%= ff.collection_select :type_editeur_id, TypeEditeur.all.order(:libelle), :id, :libelle %>
</td>
</tr>
<% end %>
<% end %>
</tbody>
</table>
...
I suppose I am missing something in either this association or (and ?) in the form’s construction but I cannot see what. I have often use has_many through
associations and created the forms without any problems but it is the first time I have three tables involved in and, clearly, I miss something.
Any help would be welcoming gladly, I still have a long way to understand clearly Rails :). French writer La Bruyère was saying : “Si claire est l’eau de ces bassins, qu’il faut se pencher longtemps au-dessus pour en comprendre la profondeur” - The water in these pools is so clear that you have to lean over it for a long time to understand its depth. So it is for Rails, isn’t it :)?