ecto_state_machine icon indicating copy to clipboard operation
ecto_state_machine copied to clipboard

Using EctoStateMachine removes Ecto.Changeset functions from the model

Open davidrichards opened this issue 9 years ago • 1 comments

By defining a state machine on a model, I can non longer define a changeset in my model. If I import Ecto.Changeset after I've defined my state machine, things tend to work well again.

I've included examples of what should fail and succeed here: https://gist.github.com/davidrichards/694c7eb030c01b17b014cbaea3aad4f1

The error I get is something along the lines of:

== Compilation error on file web/models/invitation.ex ==
** (CompileError) web/models/invitation.ex:77: undefined function cast/3
    (stdlib) lists.erl:1338: :lists.foreach/2
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6

This is the error I get from my own project, not the gist I created above.

Bottom line, I think by aliasing the Ecto.Changeset inside the macro we are changing the way we need to write changesets in our models for non-state changes. To get the import to work again after using the macro, I either have to import Ecto.Changeset again (as in the second example in the gist) or use Changeset.cast(...), Changest.put_change(...), etc.

If there's a way to make that macro less obtrusive to it's surrounding model, that would be useful.

Thanks for this work, btw.

davidrichards avatar Sep 30 '16 01:09 davidrichards

Just need to not call alias Ecto.Changeset in the macro. You can also just move your use MyApp.Web, :model to after the use EctoStateMachine ...

sitch avatar Mar 29 '17 17:03 sitch