On 6.2.2006, at 23.14, Jon S. wrote:
I was just reading through the Rails book and I got to the part about
belongs_to versus references. Shouldn’t references be allowed as a
synonym to belongs_to?
Looking at this from a schema in the model view point, belongs to
corresponds to a foreign key with the cascade on delete option.
References is just a normal forgeign key with no cascade option.
belongs_to is just a way to state in Ruby (about) the same you use
foreign keys in RDBMS’s. It doesn’t lock you into any cascading
option per se. It also gives you a lot more, as you probably have
seen in the book. You can use explicit foreign keys in the db (for
enhanced data integrity) but that doesn’t mean you can leave away the
belongs_to and friends from the AR models. They have more tasks than
just defining a relationship, e.g. being macros that create all the
needed methods like @author.books etc. that make AR such a pleasure
to use instead of raw SQL.
While it might to some extent be possible to mimic this by exploring
the db schema (like the normal attribute methods for AR models are
used), this could never be as complete as the current implementation.
Besides, support and mechanisms for foreign keys differs quite a bit
from db to db.