Forum: Ruby on Rails belongs_to versus references

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jon S. (Guest)
on 2006-02-06 23:16
(Received via mailing list)
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.

--
Jon S.
removed_email_address@domain.invalid
Jarkko L. (Guest)
on 2006-02-07 17:02
(Received via mailing list)
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.

Jon,

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.

//jarkko

--
Jarkko L.
http://jlaine.net
http://odesign.fi
This topic is locked and can not be replied to.