0

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 :)

1
  • can you show the Membership model content? Commented Feb 22, 2013 at 13:21

2 Answers 2

2

You could do this with a custom join. Using your SQL above:

Record.joins('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')

With a little work you could add this as a method within your model, make it accept parameters etc.

The Railsguide here gives a few examples of using .join

2
  • Close! You mean Record.joins, I'll give this a shot and see how it works. Is there no way to do a relationship like this? With an "OR" multi-foreign-key?
    – Volte
    Commented Feb 22, 2013 at 13:22
  • Doh! Updated my answer. I can't see how you could do what you want with Rails and one query unless you do the custom join as above, and code a method around that. That's the beauty and the downside of Rails - convention over configuration :) Commented Feb 22, 2013 at 15:38
0

Ok! Hey, thanks a lot for your help, @RiPuk. You spurred some thought on my point. What I did was create a view out of a select statement:

AS SELECT
  `records`.`id` AS `record_id`,
  `memberships`.`group_id` AS `group_id`
FROM (`records` join `memberships`) where ((`records`.`subject_membership_id` = `memberships`.`id`) or (`records`.`recorder_membership_id` = `memberships`.`id`) or (`records`.`subject_membership_id` = `memberships`.`id`));

I named this view view_group_records, then created an ActiveRecord model called GroupRecord (with the appropriate relationships) and now in my Group model, I set up a has_many relationship to GroupRecord, and then has_many :records, :through => :group_records.

This seems to work out well!

But your solution definitely works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.