Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails: create a relationship that combines three other relationships
    primarykey
    data
    text
    <p>I'm attempting to create a relationship that combines three other relationships. The initial three are all of the same class, I'd like like a conglomerate, however I can't find a nice "arel" way of doing it.</p> <p>Here is an example of what I'm trying to do.</p> <p>The SQL query that works is</p> <pre><code>SELECT `records`.* FROM `records` INNER JOIN `memberships` ON `records`.`subject_membership_id` = `memberships`.`id` OR `records`.`recorder_membership_id` = `memberships`.`id` OR `records`.`subject_membership_id` = `memberships`.`id` WHERE `memberships`.`group_id` = 4 </code></pre> <p>Of course, I want to do that the rails way. Here is what I have so far. It should be pretty self-explanitory, so I won't waste your time explaining it further.</p> <pre><code>class Group &lt; ActiveRecord::Base has_many :recorded_records, :through =&gt; :memberships, :source =&gt; :recorded_records, :class_name =&gt; "Record" has_many :reported_records, :through =&gt; :memberships, :source =&gt; :reported_records, :class_name =&gt; "Record" has_many :reviewed_records, :through =&gt; :memberships, :source =&gt; :reviewed_records, :class_name =&gt; "Record" #has_many :records through recorded_records, reported_records, reviewed_records?? # Psuedo code above... Wondering how best to do this. I've tried... def records recorded_records | reported_records | reviewed_records end # But that makes three seperate database queries when what I really want is... # SELECT `records`.* FROM `records` INNER JOIN `memberships` ON `records`.`subject_membership_id` = `memberships`.`id` OR `records`.`recorder_membership_id` = `memberships`.`id` OR `records`.`subject_membership_id` = `memberships`.`id` WHERE `memberships`.`group_id` = 4 end </code></pre> <p>You guys rock! Thanks :)</p>
    singulars
    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