Forum: Ruby on Rails belongs_to or has_many

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.
Ead43bcb0202410540a6a7cee5955d46?d=identicon&s=25 Fresh Mix (giga)
on 2009-02-21 22:39
2 tables Items and Categories

Categories (id, name)
Items (id, name, category_id)

Category_id can be null, and there are Categories that has not an Item.
D93ae163042e0a937941e0ba4d8ed5d7?d=identicon&s=25 Eric (Guest)
on 2009-02-21 22:44
(Received via mailing list)
Any chance of taking this list moderated?
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-22 00:41
(Received via mailing list)
On Feb 21, 9:39 pm, Jo Jo <rails-mailing-l...@andreas-s.net> wrote:
> 2 tables Items and Categories
>
> Categories (id, name)
> Items (id, name, category_id)
>
> Category_id can be null, and there are Categories that has not an Item.

Well you may not realise it yet, but the fact that having a
category_id on the items table  and no other columns on the categories
table works for you means that you have already solved this problem:
the belongs_to always lives on the table with the foreign key and the
has_many or has_one on the other side.

Fred
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-02-22 00:41
(Received via mailing list)
On Feb 21, 11:34 pm, Frederick Cheung <frederick.che...@gmail.com>
wrote:
> category_id on the items table  and no other columns on the categories
> table works for you means that you have already solved this problem:
> the belongs_to always lives on the table with the foreign key and the
> has_many or has_one on the other side.
>
Oops, forgot to add that
http://api.rubyonrails.org/classes/ActiveRecord/As...
and http://guides.rails.info/association_basics.html cover this sort
of stuff.

Fred
This topic is locked and can not be replied to.