swraife
swraife

Reputation: 93

Rails left outer join on polymorphic association

I'm using the PublicActivity gem. I'm trying to eager load the associated activities for a group of users. I've set up the 'activist' association per their documentation in my user class, which defines this:

 def activist
  has_many :activities_as_owner,
   :class_name => "::PublicActivity::Activity",
   :as => :owner
  has_many :activities_as_recipient,
   :class_name => "::PublicActivity::Activity",
   :as => :recipient
 end

My query in the controller is:

@results = @action_plan.users.includes(:activities_as_owner).where(activities: {recipient_id: @action_plan.id, key: "action_plan_role.access_granted" }).references(:activities_as_owner)

This does eager load the activities, but it does not include a user if they don't have a corresponding activity, which is not what I want. According to ActiveRecord guides, including the '.references(:activities_as_owner)' should make it an OUTER join instead INNER, so it returns all the users regardless of whether or not they have an activity. In place of ':activities_as_owner', I've tried :activities, :activist, :all, etc, but nothing I can think of does the trick.

Squeel or AREL answers are more than welcome as well!

Upvotes: 2

Views: 580

Answers (0)

Related Questions