Guilty by association

No apologies for the punny title!

Anyway, here’s the thing. I know what I want to do but not sure of the
right way to model it.

So far I have this in place.

has_many :names

belongs_to :user
Name.given Name.gender Name.user_id

So basically a User has a list of names that they manage some male and
some female.

Now what I’d like to do is this in plain English. I want to allow
registered users to be able to comment on each others name lists,
importantly not the individual names the lists of the names.

So I’ve worked out I need a Comment model and a User should have_many
Comments, so a comment needs a Body and a user_id - thing is the comment
belongs_to a User and it belongs_to other users… not sure if this
makes sense… can anyone deconfuse me ?

Hey Bingo Bob.

It isn’t EXACTLy what you want, but both should lead you down a better
with associations etc., Hope that helps. Good Luck


On Tue, Mar 20, 2012 at 10:55 AM, bingo bob [email protected]

“Ruby on Rails: Talk” group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at

Sincere Regards,
Kisha Richardson

Thanks Kisha - the links are useful, I was aware of them both by despite
having a crack at this in the console (I find that’s a great place to
try stuff out and experiment I can’t get there).

Here’s another way of looking at it, the output/result I need.

Given the schema above I need the result of the query/scope/find_all_by
to look something like this.

If we’ve got records like


I need a method to return all this (I guess as an array of hashes so I
can access the attributes?).

Rod (3, male)
Freddy (2, male)
Jane (1, female)
Mary (1, female)

So that’s unique and with this order…

  1. By occurences (most common first)
  2. By name in alpha order

I need access to the name and gender attributes as well as the count
(which clearly is not an attribute).

The end game is so that I can easily query my data sets and work out the
10 most popular names across the users.

At a later date I’d also love to be able to move on from this and find
stuff that’s “trending”, for example what’s changing position the most
in a given time period or what’s most popular this week, last week, last
month, last year. All that stuff and variants thereof.

What’s blocking me most is whether I should build small methods/scopes
and chain them together OR one large “do it all in one go” method/scope.
Not to mention how to write it.

Most grateful for any insight.