Forum: Ruby on Rails when (not) to use belongs_to

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.
unknown (Guest)
on 2006-03-24 01:42
(Received via mailing list)
I know you get a lot of questions like this, but I couldn't find one
which
answers exactly what I'm after. I'm reasonably new to rails, and I'm
having
a bit of trouble with when to use belongs_to. On the rails wiki (
http://wiki.rubyonrails.org/rails/pages/belongs_to), it says "In
general,
the Foo model belongs_to :bar if the foo table has a bar_id foreign key
column." But according to ForumExample (
http://wiki.rubyonrails.org/rails/pages/ForumExample) doesn't always do
this. A Message has_one author but there's no 'belongs_to :message' in
the
Author class. There are examples of when the author uses it, and
examples of
when he doesn't.
Another thing that confused me was the fact that the DBModel page on the
wiki says "Note that you'll never need to label a link belongs_to รข??
relationships are defined from the source table viewpoint, so the
belongs_tois implicit in the relationship." I wasn't sure if that's
just referring to
use of this particular gem (which I can't get to work, but I've managed
without) or generally.
Any help much appreciated.
-Nathan
Chris C. (Guest)
on 2006-03-24 02:23
unknown wrote:
> the Foo model belongs_to :bar if the foo table has a bar_id foreign key
> column." But according to ForumExample (
> http://wiki.rubyonrails.org/rails/pages/ForumExample) doesn't always do
> this. A Message has_one author but there's no 'belongs_to :message' in
> the
> Author class. There are examples of when the author uses it, and
> examples of
> when he doesn't.

Yes, a Message has_one author because when displaying the message you'll
need to put some informations about his author (name, email).
But, maybe you'll never need to list every message written by an author,
in that case you don't need to use an association between Author and
Message (even if it seems sensible and useful, but that's just for the
example).

BTW, the relation between Author and Message would not be a belongs_to
but has_many.

So, if one sided you'll have
Message has_one Author
And if you want it two sided you'll have also
Author has_many Message
Justin F. (Guest)
on 2006-03-24 03:01
(Received via mailing list)
removed_email_address@domain.invalid wrote:

> On the rails
> wiki ( http://wiki.rubyonrails.org/rails/pages/belongs_to), it says "In
> general, the Foo model belongs_to :bar if the foo table has a bar_id
> foreign key column." But according to ForumExample (
> http://wiki.rubyonrails.org/rails/pages/ForumExample) doesn't always do
> this. A Message has_one author but there's no 'belongs_to :message' in
> the Author class.

The ForumExample looks wrong. I would expect to see Author has_many
:messages and Message has_one :author.

> Another thing that confused me was the fact that the DBModel page on the
> wiki says "Note that you'll never need to label a link |belongs_to| ?
> relationships are defined from the source table viewpoint, so the
> |belongs_to| is implicit in the relationship." I wasn't sure if that's
> just referring to use of this particular gem (which I can't get to work,
> but I've managed without) or generally.

That's specific to the use of the dbmodel gem.

regards

   Justin
Justin F. (Guest)
on 2006-03-24 03:33
(Received via mailing list)
Justin F. wrote:
> The ForumExample looks wrong. I would expect to see Author has_many
> :messages and Message has_one :author.
                         ^^^^^^^ Oops! I meant belongs_to
This topic is locked and can not be replied to.