Hello, it basically works as follows,
I got the official documentation too link
collection_select(:post, :author_id, Author.all, :id, :name_with_initial, prompt: true)
:post
= > Name of your select is usually the class of your model too
:author_id
= > Field of your model (in the data table, that Post
is with the belongs_to :author
binding), which will generate the field name for example: post [author_id]
Author.all
= > It is the list with all the authors in the database, the .all method of a class inherited by ActiveRecord, goes there in the database and searches all the values of this table
:id
= > You will get the ID of the fields to fill the value of select, which is the value that will go to the controller when submitting the form
:name_with_initial
= > It is taking in the Model Author this class, which is the name first name + the second name, it is what will appear in the home option of the select field
:prompt: true
= > It will have an "empty" option in the select, this is fine, because if it does not, it will be the first value found with Author.all
and the person can select "anything" and not even realize that this field was filled with wrong value