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.
Here is an example of what I'm trying to do.
The SQL query that works 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
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.
class Group < ActiveRecord::Base
has_many :recorded_records, :through => :memberships, :source => :recorded_records, :class_name => "Record"
has_many :reported_records, :through => :memberships, :source => :reported_records, :class_name => "Record"
has_many :reviewed_records, :through => :memberships, :source => :reviewed_records, :class_name => "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
You guys rock! Thanks :)
Membership
model content?