Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The other answers appear to be neglecting memberships that you mentioned. If those are actual objects which you have a recording of, then what you choose to do depends on the size of your tables. If they aren't terribly huge, then the "more OO" solution would probably look something like this:</p> <pre><code>class Project &lt; ActiveRecord::Base has_many :memberships has_many :users, :through =&gt; :memberships def user_companies self.users.map {|user| user.companies}.flatten.uniq end end class Membership &lt; ActiveRecord::Base belongs_to :user belongs_to :project end class User &lt; ActiveRecord::Base has_many :memberships has_many :projects, :through =&gt; :memberships belongs_to :company end class Company &lt; ActiveRecord::Base has_many :users end </code></pre> <p>This might not perform great, as it pulls a lot of data from the database and then does all the filtering in memory, but it's pretty intuitive to read. If you wanted to shove all the computing down into the database, I think a good solution would likely look something like this in the Project class instead:</p> <pre><code>def user_companies Company.find_by_sql("SELECT company.* FROM companies, users, memberships WHERE companies.id = users.company_id AND users.id = memberships.user_id AND memberships.project_id = #{self.id}") end </code></pre> <p>It's a little less clean but will put most of the processing nearest the data, and at only a three table join should not end up generating such a huge number of tuples that your DBMS falls apart at the seems.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload