3 ms·
Can you print an example of the random test error? I run a decent sized phoenix project (150K lines) and likely can help you out if you give a bit more detail.
by pg_bot 8y ago
Can you print an example of the random test error? I run a decent sized phoenix project (150K lines) and likely can help you out if you give a bit more detail.
With regard to associations, for simple belongs_to associations I will usually write separate changeset functions where the first argument is pattern matched against the record. I will give an example with Post/Comment
defmodule MyApp.Blog.Comment do
def changeset(%Post{} = record, params) do
record
|> build_assoc(:comments)
|> changeset(params)
end
def changeset(%Comment{} = record, params) do
record
|> cast(params, [:body])
|> validate_required([:body])
# Continue with validations, constraints, etc.
end
end
In this case the changeset function calls itself once it handles building the association. For more complex conditions, I will usually write a "Form" module that may handle that case usually with a Enum.reduce combining multiple calls to `put_assoc` so that I can make nested associations in one insert.
Hope this helps a bit.
- enraged_camel 8y ago>>def changeset(%Comment = record, params) do Small typo: should be %Comment{}
- pg_bot 8y agothanks, fixed