Pass in user or user_id to functions

When you are creating new functions within Elixir and Ecto, is it standard to pass in a struct or just an id to look up within the function. For example if I were to create an update user function, would most people create a function that accepts the user_id or the entire user?

1 Like

I’m hardly an Ecto pro yet but I’m favoring IDs, especially for inserts.

For example I have this one schema that has 3 foreign keys. It feels natural to pass in the IDs because it makes it easier to test (I can just pop in an arbitrary ID number instead of needing the full Ecto struct). Then I just pass in those params to a changeset and it can do all sorts of interesting foreign key and unique constraint validations with nothing more than IDs.


If your boss want to have a talk with you, do you go to his office or do you just send your mouth and ears to his office?

I tend to pass around the integrity instead of the parts whenever possible. It’s easy to extract parts from the whole thing, but it can be hard if not impossible to reconstruct the whole thing from the parts.

I usually accept both, either the ID or the structure with the ID that just calls down to the base ID version unless having the structure can make something more efficient otherwise.