On 13 January 2012 11:49, bingo bob <[email protected]> wrote: > Hoping someone might be able to throw me a few clues on this one. > > I'm working with lists of names a User (current_user) manages lists of > names, some male some female (scoped on m or f in gender), it's all set > up such that I can do things like this - all working out fine. > > current_user.names.male > (returns a list of the current users male names, pretty standard stuff I > believe). > > a Name has the following attributes > > given:string > gender:string > postion:integer > user_id:integer > > > What I'd like to do next is a little scary for me but I'm sure possible, > it is list comparison. > > Basically for any given name, I'd like a method that returns other names > from *other users lists* where they have that given name in the list > (phew what a mouthful, not sure if that's explained correctly, I think > so). > > For example, I'd like to do this. > > given user Bob has a list of male names like this > > robert > martin > frank > harry > > and user Sally has a list of male names like this > > keith > harry > paul > > name = User.find_by_username('Bob').names.first > => 'robert' > > what I'd like is a method like this > > name.get_other_users_names > > which would return nil as no other users have 'robert' > > however > > name = User.find_by_username('Bob').names.last > => 'harry' > > name.get_other_users_names > This should return keith and paul from Sally's list.
You have not told us what is the association between user and name, is it has_many belongs_to or has_and_belongs_to_many? The difference is crucial because if it is has_and_belongs_to_many then if you have a name then name.users will give you all the users that have that name, which is half way to what you want I think. Colin -- You received this message because you are subscribed to the Google Groups "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 http://groups.google.com/group/rubyonrails-talk?hl=en.

