Skip to content

Commit 20d1607

Browse files
authored
Merge pull request #322 from justindell/where-with-ranges-fix
Correct endless range example and provide some clarity
2 parents c5f24e4 + b720777 commit 20d1607

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

README.adoc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,21 +1061,26 @@ Use ranges instead of defining comparative conditions using a template for scala
10611061
[source,ruby]
10621062
----
10631063
# bad
1064-
User.where("created_at > ?", 30.days.ago).where("created_at < ?", 7.days.ago)
1065-
User.where("created_at > ? AND created_at < ?", 30.days.ago, 7.days.ago)
1066-
User.where("created_at > :start AND created_at < end", start: 30.days.ago, end: 7.days.ago)
1064+
User.where("created_at >= ?", 30.days.ago).where("created_at <= ?", 7.days.ago)
1065+
User.where("created_at >= ? AND created_at <= ?", 30.days.ago, 7.days.ago)
1066+
User.where("created_at >= :start AND created_at <= :end", start: 30.days.ago, end: 7.days.ago)
10671067
10681068
# good
10691069
User.where(created_at: 30.days.ago..7.days.ago)
10701070
10711071
# bad
1072-
User.where("created_at > ?", 7.days.ago)
1072+
User.where("created_at >= ?", 7.days.ago)
10731073
10741074
# good
10751075
User.where(created_at: 7.days.ago..)
10761076
1077-
# okish - there is no range syntax that would denote inclusion on one end and
1078-
# exclusion on another.
1077+
# note - ranges are inclusive or exclusive of their ending, not beginning
1078+
User.where(created_at: 7.days.ago..) # produces >=
1079+
User.where(created_at: 7.days.ago...) # also produces >=
1080+
User.where(created_at: ..7.days.ago) # inclusive: produces <=
1081+
User.where(created_at: ...7.days.ago) # exclusive: produces <
1082+
1083+
# okish - there is no range syntax that would denote exclusion at the beginning of the range
10791084
Customer.where("purchases_count > :min AND purchases_count <= :max", min: 0, max: 5)
10801085
----
10811086

0 commit comments

Comments
 (0)