mguimas
The rationale behind the `belongs_to` name
If a schema can have several belongs_to associations with different other schemas then to which of these does it really belong?
For me, the belongs_to name implies the idea of aggregation between schemas, but an aggregate is something indivisible, so the fact that something can have several belongs_to associations does not seem to make sense.
Where does the belongs_to name come from?
What is our opinion about this?
Thanks
Most Liked
brightball
It’s been around for a long time but personally I always preferred the simple “one to many”, “many to one”, “many to many” and “one to one”.
Rails was very big on language expression when it first came out and there are entire rule sets around pluralization of object names, pluralizer libraries that account for language exceptions, etc to work within that model.
It is easier to say “A User belongs to an Account” than “Users are many to one with Accounts”. The inverse is fine “An Account has many Users”.
The issue was around key placement. If you say “A User has one Account” the language is fine but you don’t know where the key is stored. Rails went with “A User belongs to an Account” to mean the user table has an account id and “A User has one Account” to mean the Account table has a User id.
benwilson512
Can you elaborate on how you’re relating an ecto schema or set of schemas to the notion of an aggregate? Is a single schema an aggregate? a collection of them?
mguimas
I think you have answered well to my original question, making clear what was the idea of introducing the belongs_to name. This name solved the problem you mention, nevertheless it causes confusion when thinking on aggregation between entities.
I don’t know if the idea of aggregates existed by the time Rails was introduced, if it existed but Rails’ authors were not aware of it, or if they were aware but ignored that choosing belongs_to would conflict with such idea. Perhaps they thought about it but did not find anything better than belongs_to that would solve the key placement problem, without conflicting with the aggregate idea.
Perhaps a better nomenclature exists …, perhaps it exists not.
Thank you.
Popular in Discussions
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









