pillaiindu

pillaiindu

What is the difference between using `:references` and `:belongs_to` in a generate command in Rails?

What is the difference between using :references and :belongs_to in the following command?

bin/rails generate scaffold LineItem product:references cart:belongs_to

BTW it’s from the book Agile Web Development with Rails 7 (PragProg) by @rubys @pragdave.

Thank you!

Marked As Solved

rubys

rubys

Author of Agile Web Development With Rails

In some sense, there is no difference: if you look at the code, they actually are aliases of one another:

So they are synonyms at the database level.

At a semantic level, belong to implies a level of ownership. You can think of the cart as “owning” a line item. This is different than the relationship between a line item and a product… a product has an independent existence, and doesn’t “own” line items.

If this makes sense to you, then this is just one of the many ways that Rails helps you express your intent when defining a model. If it doesn’t - don’t worry to much about it, because ultimately these two terms are synonyms.

Also Liked

pillaiindu

pillaiindu

Thank you @rubys !

This makes sense now.

Popular Backend topics Top

PragmaticBookshelf
Don’t accept the compromise between fast and beautiful: you can have it all. Phoenix creator Chris McCord, Elixir creator José Valim, and...
New
PragmaticBookshelf
TDD is a modern programming practice that all C developers need to know. It’s a different way to program—unit tests are written in a tigh...
New
DevotionGeo
How Dgraph was running out of memory for some users, and how Go’s Garbage collector wasn’t enough, and Dgraph team used jemalloc to manag...
New
First poster: bot
Ruby on Rails v6.1.0 has been released. Link: Release 6.1.0 · rails/rails · GitHub
New
ohm
Does anybody have good learning resources with regards to going into Event Driven Design, Architecture or Sourcing? I got recommended Er...
New
AstonJ
More cool tech inspired by <span class="hashtag-icon-placeholder"></span>erlang :nerd_face: By combining the fault-tolerance and massiv...
New
joshi
Hey everybody! I’m working on the project that includes import of Oracle data to PostgreSQL. That data comes as Oracle export (expdp) fi...
New
PragmaticBookshelf
Leverage Elixir and the Nx ecosystem to build intelligent applications that solve real-world problems in computer vision, natural languag...
New
PragmaticBookshelf
Create Android applications using Jetpack Compose 1.6, Android Studio, Material Design 3, and the Kotlin programming language. Neil...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New

Other popular topics Top

AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
PragmaticBookshelf
A PragProg Hero’s Journey with Brian P. Hogan @bphogan Have you ever worried that your only legacy will be in the form of legacy...
New
brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
foxtrottwist
Here’s our thread for the Keyboardio Atreus. It is a mechanical keyboard based on and a slight update of the original Atreus (Keyboardio ...
New
Rainer
Not sure if following fits exactly this thread, or if we should have a hobby thread… For many years I’m designing and building model air...
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
New