Insert with associated data, selecting associated data in changeset

Hello all,

I’m trying to implement oauth server and I’m not sure how to implement inserting token into database. For simplicity I have this schema:

token
  client_id
  ...

scope
  name (string)
  ...

token_scope
  token_id (uuid)
  scope_id (uuid)

Create token should accept this kind of data:

token
  scopes: "scope1 scope2"

And my question, how to model this kind of insert using changeset? I have came up with some solutions:

  • preprocess params, select all required scopes and pass selected scopes to changeset
  • create everithing using Ecto.Multi (I have little bit of issue with multi mostly because of error reporting)
  • resolve it in changeset using prepare_changes

What do you think is the best solution?