Advertisement
Guest User

Untitled

a guest
Jul 11th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. # Create the user. Note that the (empty) `organizations` field has to be preloaded.
  2. user_map = %{display_name: "User 1", login_name: "1@y.com", password: "password"}
  3. changeset = User.password_setting_changeset(%User{}, user_map)
  4. user = Repo.insert!(changeset) |> Repo.preload(:organizations)
  5.  
  6. # Do the same for the organization:
  7. org = %Eecrit.Organization{short_name: "org1", full_name: "Organization 1"}
  8. org = Repo.insert!(org) |> Repo.preload(:users)
  9.  
  10. # Update one of the two of them:
  11. changeset = Ecto.Changeset.change(user) |> Ecto.Changeset.put_assoc(:organizations, [org])
  12.  
  13. # "When you save this change to the user, the join table will have its foreign keys populated in both directions."
  14. Repo.update!(changeset)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement