Skip to content

Commit

Permalink
activerecord: Add AR::Relation#sanitize_sql_like (ruby#691)
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).

This allows to use sanitize_sql_like and its family via AR::Relation.
It is often used in the scope definition.
  • Loading branch information
tk0miya authored Oct 24, 2024
1 parent 28a250a commit f745a00
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 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,7 @@ class User < ActiveRecord::Base
validates :name, presence: true, if: -> { something }
validates :age, presence: true, if: ->(user) { user.something }

scope :name_like, ->(name) { where(arel_table[:name].matches("%#{sanitize_sql_like(name)}%")) }
scope :matured, -> { where(arel_table[:age].gteq(18)) }

before_save -> (obj) { obj.something; self.something }
Expand Down
5 changes: 5 additions & 0 deletions gems/activerecord/6.0/activerecord.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@ module ActiveRecord
# user.name # => Oscar
def new: (?untyped? attributes) ?{ () -> untyped } -> untyped

# ActiveRecord::Relation delegates method calls to a ActiveRecord::Base class.
# To represent the behavior in the RBS system, we define and include the class methods of ActiveRecord::Base here.

include Sanitization::ClassMethods

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

0 comments on commit f745a00

Please sign in to comment.