Which associations?

I thought I was doing ok, then started reading about rich joins and now
I'm all confused again :-/ (I'm a nube)

Would really appreciate someone quickly telling me the best associations
to use for the following scenario...

Models:

- User
- Topic (think of topics as 'categories')
- Comment

- TopicAssociations (for a rich join?)

Any user can submit a 'topic'
Any user can leave a 'comment' for a topic (one per topic, but to as
many topics as they want)

A user's profile will display all of the topics that interest them (and
the comments they left for it)
Each topic page will display all comments left for it (as well as all
users interested in it)

(I'll want to count which topics are the most popular, too)

Associations:

- User model
(Do I need a rich join?)
has_many :topic_associations
has_many :topics, :through => :topic_associations
has_many :comments

- Topic model
has_many :topic_associations
has_many :users, :through => :topic_associations
has_many :comments

- TopicAssociations
belongs_to :user
belongs_to :topic

- Comment model
belongs_to :user
belongs_to :topic

Does that make sense? Or am I on the wrong track? I have a feeling I am
over-confusing things.

I thought I was doing ok, but evidently, not :frowning:

Aston J. wrote in post #968178:

I thought I was doing ok, then started reading about rich joins and now
I'm all confused again :-/ (I'm a nube)

What's a rich join? Are you talking about join tables that contain
additional information?

Would really appreciate someone quickly telling me the best associations
to use for the following scenario...

Models:

- User
- Topic (think of topics as 'categories')

Why not call them categories, then?

- Comment

- TopicAssociations (for a rich join?)

Any user can submit a 'topic'
Any user can leave a 'comment' for a topic (one per topic, but to as
many topics as they want)

A user's profile will display all of the topics that interest them (and
the comments they left for each of them)
Each topic page will display all comments left for it (as well as all
users 'interested' in it)

(I'll want to count which topics are the most popular, too)

Associations:

- User model
(Do I need a rich join?)

Only if you need to store additional information in the association. If
not, habtm will work fine.

has_many :topic_associations
has_many :topics, :through => :topic_associations
has_many :comments

- Topic model
has_many :topic_associations
has_many :users, :through => :topic_associations
has_many :comments

- TopicAssociations
belongs_to :user
belongs_to :topic

I'd probably call this model Interest, not TopicAssociation. And you
may not need it at all, if habtm will work for your use case (which I
can't tell from the information you provided).

- Comment model
belongs_to :user
belongs_to :topic

Does that make sense? Or am I on the wrong track? I have a feeling I am
over-confusing things.

I thought I was doing ok, but evidently, not :frowning:

Actually, you are. This looks pretty good. Where do you think you're
going wrong?

Best,

Thanks for the reply Marnen.

It would probably help if I give a bit more info about the site...

The site's aim is to list 'popular' topics. My users will be able to
create a topic (if it does not already exist) and leave a reason/comment
about why they like/dislike that topic. They can select as many topics
as they want, but only leave one comment per topic.

The topics homepage will list all the most popular topics (ie topics
with the most users selecting it/leaving a comment)

The users own profile page will simply list the topics they have chosen,
and the comment they left for each of them.

I thought it would be a nice easy one to do as my first Rails app :lol:

//I'll add that info to the first post too//

I think I agree with you about the rich-join, a habtm would probably
work fine. The reason for that was because someone suggested calculating
'popularity' in that model - but looking back through it now, I think I
could just count the number of comments per topic to see which topic is
the most popular.

Please quote when replying.

Aston J. wrote in post #968185:

Thanks for the reply Marnen.

It would probably help if I give a bit more info about the site...

The site's aim is to list 'popular' topics. My users will be able to
create a topic (if it does not already exist) and leave a reason/comment
about why they like/dislike that topic. They can select as many topics
as they want, but only leave one comment per topic.

The topics homepage will list all the most popular topics (ie topics
with the most users selecting it/leaving a comment)

The users own profile page will simply list the topics they have chosen,
and the comment they left for each of them.

I thought it would be a nice easy one to do as my first Rails app :lol:

Then everything sounds OK so far. Again, where do you think you're
going wrong?

//I'll add that info to the first post too//

I think I agree with you about the rich-join, a habtm would probably
work fine. The reason for that was because someone suggested calculating
'popularity' in that model - but looking back through it now, I think I
could just count the number of comments per topic to see which topic is
the most popular.

You certainly could (check out ActiveRecord::Calculations for the
abstraction layer that deals with SQL aggregate functions).
counter_cache exists as a performance hack on top of that.

But in general, YAGNI. Design for what you actually have now, not for
what you *might* have in the future, except in those (astoundingly few)
cases where it won't be feasible to add it later.

Best,

Thanks again Marnen.

I guess I'm worried about the associations and two foreign keys for the
comment model - as I've put it in my first post, are the has_many
:comments, belongs_to etc ok?

/Sorry for being such a nube - and thanks for your patience./

Again: please quote when replying!

Aston J. wrote in post #968194:

Thanks again Marnen.

I guess I'm worried about the associations and two foreign keys for the
comment model - as I've put it in my first post, are the has_many
:comments, belongs_to etc ok?

/Sorry for being such a nube - and thanks for your patience./

Why are you worried? Instead of asking, try it and see what happens.
If it's hard, your data model is probably wrong. If your automated
tests fail, something is wrong. If it works and your data is decently
normalized (which it looks like it will be), don't worry.

Best,

Marnen Laibow-Koser wrote in post #968199:

Again: please quote when replying!

Aston J. wrote in post #968194:

Thanks again Marnen.

I guess I'm worried about the associations and two foreign keys for the
comment model - as I've put it in my first post, are the has_many
:comments, belongs_to etc ok?

/Sorry for being such a nube - and thanks for your patience./

Why are you worried? Instead of asking, try it and see what happens.
If it's hard, your data model is probably wrong. If your automated
tests fail, something is wrong. If it works and your data is decently
normalized (which it looks like it will be), don't worry.

Best,
--
Marnen Laibow-Koser
http://www.marnen.org
marnen@marnen.org

Sent from my iPhone

Hi Marnen - sorry I didn't notice the thing about quoting *blush*

I will give it a try and see how it goes - hopefully it'll all click...
eventually lol

Thanks again for your help :slight_smile: