Phoenix n00b here.
So I have a logged in current user (using Coherence, but I don't think this has impact here):
user = Coherence.current_user(conn)
And I have an object that has a relationship with a user:
object = Repo.get(Object, XX) |> Repo.preload(:user)
owner = object.user
I simply want to check that user is the same as owner.
Writing
user == owneris not correct because - I guess - structs are references so the two will be difference despite being the same DB object.Writing
user.id == owner.idshall work but sinceusercan benilit will fail in that case, while the equality remains true.I tried
user[:id] == owner[:id]since this doesn't fail ifuseris nil but then if it is auser, I get aUser does not implement the Access behaviourerror. :(Writing
not is_nil(user) && user.id == owner.idworks but is ugly (and thanks godowneris not also nillable)
I guess I'll need a helper function here, but ain't it already built-in? If so, where? In Ecto? In Coherence?
Ain't there a way to override struct equality for some specific structs?
user == ownershould work assuming it is actually being pulled from the same table and are coming from the same module, with the caveat that the data could have changed between asking for theuserand asking for theowner, so it is theoretically possible to have it be false, even if they have the same id's. - Justin WoodRepo.preloadsomewhere, it fails, which is not acceptable (as those remain the same objects). - Augustin RiedingerEcto.Schema? - Augustin Riedinger