I'm working on the complex registration form. My users will have their profiles with contact info and collections of items. My schemas (User, Profile, Phones, Collections, Items) look like this:
defmodule MyProject.User
schema "users" do
field :email, :string
field :name, :string
has_many :profiles, MyProject.Profile, on_delete: :delete_all
has_many :collections, through: [:profiles, :collections]
timestamps()
end
end
defmodule MyProject.Profile
schema "profiles" do
field :address, :string
field :skype, :string
belongs_to :user, MyProject.User
has_many :phones, MyProject.Phone, on_delete: :delete_all
has_many :collections, MyProject.Collection, on_delete: :delete_all
timestamps()
end
end
defmodule MyProject.Phone
schema "phones" do
field :phone, :string
belongs_to :profile, MyProject.Profile
end
end
defmodule MyProject.Collection
schema "collections" do
field :name, :string
has_many :items, MyProject.Item, on_delete: :delete_all
timestamps()
end
end
defmodule MyProject.Item
schema "items" do
field :name, :string
field :type, :string
field :url, :string
belongs_to :collection, MyProject.Collection
timestamps()
end
end
Now I need to build complex registration form which allows to create user and his profile with some phones and one collection with some items in it all at once, with one form submit. As the table registration doesn't exist I thought that I need to use embedded_schema for this, as it's not tied to the database table:
defmodule MyProject.Registration
embedded_schema do
end
end
But I'm not sure how to describe all nested associations inside of it. Should I use has_one, has_many or embed_one, embed_many or neither of them, what are the implications? How to structure the form, should I use inputs_for for the nested associations? How to save all associated values to the database to preserve all relationships? First, the User needs to be saved, then his Profile, then some Phones, then the Collection and finally some Items. And all these should be tied together.