When (not) to use belongs_to

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

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

[email protected] 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. wrote:

The ForumExample looks wrong. I would expect to see Author has_many
:messages and Message has_one :author.
^^^^^^^ Oops! I meant belongs_to