• R
    Bring back private class methods accessibility in named scope · b9be64cc
    Ryuta Kamizono 提交于
    The receiver in a scope was changed from `klass` to `relation` itself
    for all scopes (named scope, default_scope, and association scope)
    behaves consistently.
    
    In addition. Before 5.2, if both an AR model class and a Relation
    instance have same named methods (e.g. `arel_attribute`,
    `predicate_builder`, etc), named scope doesn't respect relation instance
    information.
    
    For example:
    
    ```ruby
    class Post < ActiveRecord::Base
      has_many :comments1, class_name: "RecentComment1"
      has_many :comments2, class_name: "RecentComment2"
    end
    
    class RecentComment1 < ActiveRecord::Base
      self.table_name = "comments"
      default_scope { where(arel_attribute(:created_at).gteq(2.weeks.ago)) }
    end
    
    class RecentComment2 < ActiveRecord::Base
      self.table_name = "comments"
      default_scope { recent_updated }
      scope :recent_updated, -> { where(arel_attribute(:updated_at).gteq(2.weeks.ago)) }
    end
    ```
    
    If eager loading `Post.eager_load(:comments1, :comments2).to_a`,
    `:comments1` (default_scope) respects aliased table name, but
    `:comments2` (using named scope) may not work correctly since named
    scope doesn't respect relation instance information. See also 801ccab2.
    
    But this is a breaking change between releases without deprecation.
    I decided to bring back private class methods accessibility in named
    scope.
    
    Fixes #31740.
    Fixes #32331.
    b9be64cc
named.rb 7.9 KB