activerecord-hackery/squeel — pull request #323 by bigxiang (2014-07-25 03:21:07+00:00)
Title: Properly add 'AND' to generated SQL when joining through a polymorphic model

Properly add 'AND' to generated SQL when joining through a polymorphic model with the source type configuration and default scopes above Rails 4.

Fixes #270.

Examples:
```ruby
class User < ActiveRecord::Base
has_many :memberships, as: :member
has_many :groups, through: :memberships
has_many :packages, through: :groups
end

class Package < ActiveRecord::Base
has_many :memberships, as: :member
end

class Membership < ActiveRecord::Base
belongs_to :group
belongs_to :member, polymorphic: true

default_scope -> { where(active: true) }

before_create :set_active

def set_active
self.active = true
end
end

class Group < ActiveRecord::Base
has_many :memberships
has_many :users, through: :memberships, source: :member, source_type: 'User'
has_many :packages, through: :memberships, source: :member, source_type: 'Package'
end

Group.joins{users}.to_sql
# => SELECT "groups".* FROM groups
# INNER JOIN "memberships" ON "groups".id = "memberships"."group_id"
# AND "memberships"."active" = 't'
# AND "memberships"."member_type" = 'User'
# AND "memberships"."active" = 't'
# INNER JOIN "users" ON "users"."id" = "memberships"."member_id"
```