2

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!

swraife
  • 93
  • 2
  • 5
  • You have added where clause that will cause not return data, see the output SQL there will be left outer join not inner – Thorin May 17 '16 at 18:31
  • I only want to return records from the join table (activities) where that match the recipient_id & key. Users have tons of other activities that I don't need. What is currently happening is if a user doesn't have any activities matching that where clause, it doesn't return that user at all. I don't want that, I still need the user. – swraife May 17 '16 at 22:34

0 Answers0