Skip to content

Commit 65115f7

Browse files
authored
Merge pull request rails#47734 from olefriis/add-arel-node-docs-for-binding-parameters-and-plus
Add docs on Arel.sql binding parameters and +
2 parents 59f2b06 + 123c93a commit 65115f7

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

activerecord/lib/arel/nodes/node.rb

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Nodes
1818
# most nodes include a couple of useful factory methods to create subtree structures for common constraints. For
1919
# a full list of those, please refer to Arel::Predications.
2020
#
21-
# The following example creates a equality constraint where the value of the name column on the users table
21+
# The following example creates an equality constraint where the value of the name column on the users table
2222
# matches the value DHH.
2323
#
2424
# users = Arel::Table.new(:users)
@@ -36,8 +36,8 @@ module Nodes
3636
#
3737
# "users"."name" = 'DHH'
3838
#
39-
# The constraint fragments can be used with regular ActiveRecord::Relation objects instead of as Hash. The
40-
# following two example shows two ways of creating the same query.
39+
# The constraint fragments can be used with regular ActiveRecord::Relation objects instead of a Hash. The
40+
# following two examples show two ways of creating the same query.
4141
#
4242
# User.where(name: 'DHH')
4343
#
@@ -51,7 +51,7 @@ module Nodes
5151
#
5252
# == Functions
5353
#
54-
# Arel comes with built in support for SQL functions like +COUNT+, +SUM+, +MIN+, +MAX+, and +AVG+. The
54+
# Arel comes with built-in support for SQL functions like +COUNT+, +SUM+, +MIN+, +MAX+, and +AVG+. The
5555
# Arel::Expressions module includes factory methods for the default functions.
5656
#
5757
# employees = Employee.arel_table
@@ -73,7 +73,7 @@ module Nodes
7373
# Values that you pass to Arel nodes need to be quoted or wrapped in bind params. This ensures they are properly
7474
# converted into the correct format without introducing a possible SQL injection vulnerability. Most factory
7575
# methods (like +eq+, +gt+, +lteq+, …) quote passed values automatically. When not using a factory method, it’s
76-
# possible to convert a value and wrap it in a Arel::Nodes::Quoted node (if necessary) by calling +Arel::Nodes.
76+
# possible to convert a value and wrap it in an Arel::Nodes::Quoted node (if necessary) by calling +Arel::Nodes.
7777
# build_quoted+.
7878
#
7979
# Arel::Nodes.build_quoted("foo") # 'foo'
@@ -98,7 +98,21 @@ module Nodes
9898
#
9999
# # LOWER("users"."name") = 'dhh'
100100
#
101-
# Please keep in mind that passing data as raw SQL literals might introduce a possible SQL injection.
101+
# Please keep in mind that passing data as raw SQL literals might introduce a possible SQL injection. However,
102+
# `Arel.sql` supports binding parameters which will ensure proper quoting. This can be useful when you need to
103+
# control the exact SQL you run, but you still have potentially user-supplied values.
104+
#
105+
# Arel.sql('LOWER("users"."name") = ?', 'dhh')
106+
#
107+
# # LOWER("users"."name") = 'dhh'
108+
#
109+
# You can also combine SQL literals.
110+
#
111+
# sql = Arel.sql('SELECT * FROM "users" WHERE ')
112+
# sql += Arel.sql('LOWER("users"."name") = :name', name: 'dhh')
113+
# sql += Arel.sql('AND "users"."age" > :age', age: 35)
114+
#
115+
# # SELECT * FROM "users" WHERE LOWER("users"."name") = 'dhh' AND "users"."age" > '35'
102116
class Node
103117
include Arel::FactoryMethods
104118

0 commit comments

Comments
 (0)