So I think I learned something today from chatGPT that I was unaware of, but wanted to ask about it as chatGPT tends to just…make stuff up when it doesn’t know about things.
I had a form with multiple checkboxes, and every time the checkbox was clicked it was triggering a validation loop.
The original form was this:
@impl true
def handle_event("validate", %{"user_setting" => user_setting_params}, socket) do
changeset =
socket.assigns.user_setting
|> User_Settings.change_user_setting(user_setting_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
ChatGPT gave me this:
@impl true
def handle_event("validate", %{"user__setting" => user_setting_params}, socket) do
changeset =
socket.assigns.user_setting
|> User_Settings.change_user_setting(user_setting_params)
|> Map.put(:action, :validate)
{:noreply, assign(socket, :changeset, changeset)}
end
And it worked.
The explanation I was give was this:
The difference between
user__setting
anduser_setting
is in the way they represent the relationship between theUser
andSetting
models.
user_setting
represents a one-to-one relationship between aUser
and aSetting
. In this relationship, each user can have only one setting, and each setting belongs to only one user. This is typically used when the relationship between two models is exclusive and there can be only one instance of the related model for each instance of the main model.On the other hand,
user__setting
represents a many-to-one relationship between aUser
andSetting
. In this relationship, multiple settings can belong to the same user. This is typically used when the relationship between two models is non-exclusive and there can be multiple instances of the related model for each instance of the main model.
Does the above sound correct? It’s pretty difficult to find information on the significance of a double __ in a google search or looking through hexdocs as the double underscore appears all over the place
I basically want to know as a standalone double character, whats the purpose of __ in general?
Also when should I be using __ in my schema names? I have 2 schemas that automatically generated as a double space and they are both prefixed with “user”. Does ecto automatically make an association of sorts between existing tables if you use an underscore to name them?
For example if I have a “posts” schema and made a new schema called “post_info” would ecto make an association fo sorts between the two that I’m unaware of because the second schema starts with “post”?
Any info appreciated.