我在嘗試創建一個包含三個其他關係的關係。最初的三個都是同一個班級,我想要一個集團,但是我找不到一個很好的「arel」方法。Ruby on Rails:創建一個包含三個其他關係的關係
這是我想要做的一個例子。
的SQL查詢的工作原理是
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
當然,我想這樣做,軌道方式。這是我到目前爲止。它應該是非常自我解釋的,所以我不會浪費時間進一步解釋它。
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
你們搖滾!謝謝:)
您能顯示「會員」模型內容嗎? – 2013-02-22 13:21:28