I’m going around in circles with this one can’t solve it.
I have a database with a lot of Name objects in it, some have the same
given name - ok fairly normal.
Name
given (string)
gender (string) # it’s either m or f, not really relevant
position (integer) # not really relevant
user_id (integer) # it’s either m or f, not really relevant
I simply wish to return all the Names with a unique given name.
important I an array of objects returned not just the given names so I
can access all the attributes of the name when they’re returned, i.e. in
my view I need the full object to get at the user and gender etc.
I think the secret lies in distinct / uniq / find_by_sql somehow but try
as I might I just can’t find the secret sauce. For a bonus burrito I’d
like the results ordered by given name.
Be very grateful for a solution.
If it matters, rails 3.2
Name Belongs To User
User Has Many Names
My original question is still important but I’d like to expand it… How
might I answer some of these questions in code (returning arrays of name
objects of course)
- ten most popular boys names (unique and sorted by popularity)
- ten most popular girls names (unique and sorted by popularity)