Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedded association with user ? #308

Closed
ZePedroResende opened this issue Oct 22, 2019 · 7 comments
Closed

Embedded association with user ? #308

ZePedroResende opened this issue Oct 22, 2019 · 7 comments

Comments

@ZePedroResende
Copy link

Hi, i need to create a profile that is a has_one association with the user.

This needs to be create in the register form (embedded) alongside the user.

Is there a standard way to do this in pow or do i need to override the controllers ?

@ZePedroResende ZePedroResende changed the title Embedded association with user Embedded association with user ? Oct 22, 2019
@danschultzer
Copy link
Collaborator

Yeah, no need to override controllers. You just have to customize the changeset and add custom templates with the custom fields e.g. using Phoenix.HTML.Form.inputs_for/4.

If it should only update during registration then this might help you: #276 (comment)

@ZePedroResende
Copy link
Author

ZePedroResende commented Oct 23, 2019

@danschultzer thank you so much for the quick answer !
I already had changed the changeset and the template but i need to preload the profile assoc, so for what i understand for the other issue i probably need to do a custom context.

@danschultzer
Copy link
Collaborator

If it’s an embedded schema then you won’t have to preload it?

@danschultzer
Copy link
Collaborator

danschultzer commented Oct 23, 2019

Oh reading your question again, I realize you probably didn’t mean embedded schema. So yes, you can use a custom context, but this should only be necessary for update of existing users.

@ZePedroResende
Copy link
Author

@danschultzer im actually using the invitation extension with the register disable.
What would you recommend the custom context would look like with i only wanted to do the preload 😄 ?

@danschultzer
Copy link
Collaborator

This would work:

defmodule MyApp.Users do
  use Pow.Ecto.Context,
    repo: MyApp.Repo,
    user: MyApp.Users.User

  @impl true
  def get_by(clauses) do
    clauses
    |> pow_get_by()
    |> preload_profile()
  end
  
  defp preload_profile(nil), do: nil
  defp preload_profile(user) do
    MyApp.Repo.preload(user, :profile)
  end
end

But the above will preload the profile anywhere the get_by/1 is called. An alternatively would be to set up a custom invitation controller (with at least the show and update actions) to preload the profile. You would have full control over the flow that way, but it would be much more involved.

Here's an example of how you can override default controller actions with a custom controller for PowInvitation: #203 (comment)

@danschultzer
Copy link
Collaborator

I assume that the above worked. I'll close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants