Problem with two chained scopes with different joins to same table #179

barelyknown opened this Issue Oct 6, 2012 · 1 comment


None yet
1 participant

This problem is occurring on squeel 1.0.11.

I have two relationships to the same table in my model, and I'm using those two relationships in the join of two different scopes.

class Log < ActiveRecord::Base
  attr_accessible :created_by_id, :updated_by_id
  belongs_to :created_by, class_name: "User"
  belongs_to :updated_by, class_name: "User"
  scope :suggested,
    where{(created_by.can_admin_logs == false) | 
          (created_by.can_admin_logs == nil)}
  scope :not_edited,
    where{(updated_by.can_admin_logs == false) |
          (updated_by.can_admin_logs == nil)}

When those scopes are changed together, the sql is incorrect.

puts Log.suggested.not_edited.to_sql

SELECT "logs".* FROM "logs" INNER JOIN "users" ON "users"."id" = "logs"."created_by_id" INNER JOIN "users" "updated_bies_logs" ON "updated_bies_logs"."id" = "logs"."updated_by_id" WHERE (("users"."can_admin_logs" = 'f' OR "users"."can_admin_logs" IS NULL)) AND (("updated_bies_logs"."can_admin_logs" = 'f' OR "updated_bies_logs"."can_admin_logs" IS NULL))

I've tinkered with various changes to fix the updated_bies_logs problem but haven't found a solution.

The release notes for 1.0.11 looked like they addressed this, but I updated my gem and the problem is still here.

My mistake. 1.0.11 did fix this issue. I just looked at the sql after upgrading and it didn't look right but it is. Thanks for everything that you do.

barelyknown closed this Oct 6, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment