Advice on db/model relationships in RoR

13 views
Skip to first unread message

worldo...@gmail.com

unread,
Sep 25, 2013, 6:34:23 AM9/25/13
to rubyonra...@googlegroups.com
Hi there. I'm making my first rails app and taking Hartl's tutorial app as my starting point. All the features he implements are things I want.

Primarily the site wants to be a collection organising site. I'll be adding a table of ITEMS, and USERS can add those ITEMS to their collection or to their wantlist. 

What I'm trying to decide is where best to put the "who has what" information. It seems to me that doing it all in the USERS or ITEMS table might be a bit cumbersome. For instance an ITEM "has many" USERS with a long list of them, or a USER "has many" ITEMS, again with a long list.

Would it be better to use "has many through" with an OWNERSHIP or COLLECTION model/table? 
I'm thinking along the lines of
1, USER 5, ITEM 322, WANTLIST
2, USER 9, ITEM 27, COLECTION
3, USER 6, ITEM 90, COLLECTION
etc

If this is a better idea would it make more sense to give WANTLIST a seperate model/table?

I'm trying to work out what would be the most efficient way of doing it. They want to be easily indexable so that you can have "USER has 378 ITEMS" and the like.

Any pointers much appreciated.

Colin Law

unread,
Sep 29, 2013, 4:17:51 AM9/29/13
to rubyonra...@googlegroups.com
On 25 September 2013 11:34, <worldo...@gmail.com> wrote:
> Hi there. I'm making my first rails app and taking Hartl's tutorial app as
> my starting point. All the features he implements are things I want.
>
> Primarily the site wants to be a collection organising site. I'll be adding
> a table of ITEMS, and USERS can add those ITEMS to their collection or to
> their wantlist.
>
> What I'm trying to decide is where best to put the "who has what"
> information. It seems to me that doing it all in the USERS or ITEMS table
> might be a bit cumbersome. For instance an ITEM "has many" USERS with a long
> list of them, or a USER "has many" ITEMS, again with a long list.

If a user can have many items, and each item can be associated with
many users then neither of those will work.

>
> Would it be better to use "has many through" with an OWNERSHIP or COLLECTION

Yes, if the relationship requires it (see above).

> model/table?
> I'm thinking along the lines of
> 1, USER 5, ITEM 322, WANTLIST
> 2, USER 9, ITEM 27, COLECTION
> 3, USER 6, ITEM 90, COLLECTION

I don't understand what WANTLIST COLECTION and COLLECTION mean here.

> etc
>
> If this is a better idea would it make more sense to give WANTLIST a
> seperate model/table?
>
> I'm trying to work out what would be the most efficient way of doing it.
> They want to be easily indexable so that you can have "USER has 378 ITEMS"
> and the like.

current_user.items.count

Colin
Reply all
Reply to author
Forward
0 new messages