Skip to content

Commit

Permalink
activerecord: Add #arel_table to AR::Relation
Browse files Browse the repository at this point in the history
The ActiveRecord::Relation provides methods defined at AR::Base via
delegation (see ActiveRecord::Delegation).

Therefore AR::Relation also provides `#arel_table`.  It is often used in
the scope definition.
  • Loading branch information
tk0miya committed Sep 30, 2024
1 parent 4f55d83 commit 02d3958
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions gems/activerecord/6.0/_test/activerecord-generated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class User < ActiveRecord::Base
validates :name, presence: true, if: -> { something }
validates :age, presence: true, if: ->(user) { user.something }

scope :matured, -> { where(arel_table[:age].gteq(18)) }

before_save -> (obj) { obj.something; self.something }
around_save -> (obj, block) { block.call; obj.something }

Expand Down
2 changes: 0 additions & 2 deletions gems/activerecord/6.0/activerecord-generated.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -23516,8 +23516,6 @@ module Arel

def having: (untyped expr) -> untyped

def []: (untyped name) -> untyped

def hash: () -> untyped

def eql?: (untyped other) -> untyped
Expand Down
8 changes: 8 additions & 0 deletions gems/activerecord/6.0/activerecord.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ module ActiveRecord
# user = users.new { |user| user.name = 'Oscar' }
# user.name # => Oscar
def new: (?untyped? attributes) ?{ () -> untyped } -> untyped

def arel_table: () -> Arel::Table
end
end

Expand Down Expand Up @@ -654,6 +656,12 @@ interface _ActiveRecord_Relation_ClassMethods[Model, Relation, PrimaryKey]
def scope: (Symbol, ^(*untyped, **untyped) [self: Relation] -> void) ?{ (Module extention) [self: Relation] -> void } -> void
end

module Arel
class Table
def []: (untyped name) -> Arel::Attributes::Attribute
end
end

# https://github.com/rails/rails/blob/fbe2433be6e052a1acac63c7faf287c52ed3c5ba/activerecord/lib/arel/nodes/unary.rb
module Arel
module Nodes
Expand Down

0 comments on commit 02d3958

Please sign in to comment.