JoeZMar
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?
Most Liked
cnck1387
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.
OvermindDL1
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









