Forum: Ruby on Rails ActiveRecord Question across Tables

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
James A. (Guest)
on 2006-04-07 19:44
Hi All,

I know the answer to this may be simple but I cannot think of it! First
let me explain the problem.

I have the following tables and relations:
  log_entries  <n..n>  users
  log_entries  <n..1>  users
  users        <n..n>  groups

  groups < residence1, residence2 (non-normalized by design)

I would like a way to get an array of user id's (or user objects) from a
given log entry such that the array contains one element for each user
which is a member of a group which has a residence in common with the
user who created the log entry.

What I currently have is a messy list of code segments such as:

  groups = self.user.groups

  residences = []
  groups.each do { |group|
  	residences << group.residence1
  	residences << group.residence2
  }

  users = []
  residences.each do { |residence|
  	...
  }

  ...

Can anyone please explain the best (or just a better) way of doing this
please. I am falling very much in love with Rails, it is the first
framework I have really got to grips with and have heard lots about its
great community!

Many thanks in advance,

James A.s
This topic is locked and can not be replied to.