Validate offset value at call time like limit

rails/rails

ActiveRecord now validates the argument passed to offset in the same way as limit, raising ArgumentError for non‑integer values instead of silently coercing them.

背景

limit already validates its argument with Integer(value), so an invalid call such as Post.limit("abc") raises an error immediately. In contrast, offset previously stored the raw value and only performed offset_value.to_i when generating SQL, causing inputs like "abc" to become 0 and "5x" to become 5 without any warning. This discrepancy meant that pure‑SQL queries appeared to work, but any in‑memory finder path that used the stored offset would break or produce incorrect results. Aligning offset with limit eliminates this hidden source of bugs.

技術的な変更

offset! now coerces its argument with Integer(value) unless the value is nil, mirroring the existing behavior of limit!.

変更前:

def offset!(value) # :nodoc:
  self.offset_value = value
  self
end

変更後:

def offset!(value) # :nodoc:
  value = Integer(value) unless value.nil?
  self.offset_value = value
  self
end

The test suite was extended accordingly. test_invalid_offset in activerecord/test/cases/base_test.rb asserts that Topic.offset("asdfadf") raises ArgumentError, matching the existing test_invalid_limit. Additionally, activerecord/test/cases/relation/mutation_test.rb adds a #offset! mutation test confirming that the method returns the relation object and correctly sets offset_value.

設計判断

The implementation reuses the same Integer conversion pattern already employed by limit!, avoiding the introduction of a new validation helper or API surface. This choice preserves backward compatibility for callers that already pass clean numeric strings, while providing a clear, fail‑fast error for truly invalid input. The change is categorized as a consistency/clarity improvement rather than a security fix, because the prior to_i conversion already mitigated injection risks.

記事メタデータ

Generated by:
gpt-oss-120b for DiffDaily
LLM Trace:
77fbf23b

この記事はAIによって自動生成されています。内容の正確性については、必ずソースコードやPRを確認してください。

品質レビュー結果

Review Status:
承認済み
Review Count:
1回
Reviewed by:
gpt-oss-120b for DiffDaily

Review Criteria:

記事構成 ⚠ WARNING

Title, Context, Technical Detailの存在と明確さ

リード文・背景・技術的変更はあるが、まとめ(結論)セクションが欠如している。設計判断はあるが、全体を総括する結論がないためWARNINGとなる。

カスタムMarkdown構文 ⚠ WARNING

シンタックスハイライト・GitHubリンク記法の正確性

コードブロックのファイル名付きシンタックスハイライトは正しい形式。PRリンクは "[PR #57808]" と記載されており、完全に「[#123]」形式ではないため軽度の警告とした。

対象読者への適合性 ✓ PASS

エンジニア向けの適切な技術レベルと表現

専門的なRailsエンジニア向けの記述で、余計な入門的説明はなく適切。

パラグラフ・ライティング ✓ PASS

トピックセンテンス・1段落1トピック・段落長

各段落はトピックセンテンスで始まり、1段落1トピックで構成。文数も適切で、空行で区切られている。

Diff内容との照合 ✓ PASS

コードブロックとDiff内容の一致

記事中のコードブロックは提供されたDiffと完全に一致しており、変更点を正確に反映している。

技術用語の正確性 ✓ PASS

技術用語の正確な使用

使用されている技術用語(offset、limit、Integer、ArgumentError 等)はPR内容と合致しており誤用はない。

説明の技術的正確性 ✓ PASS

技術的主張の正確性と論理性

技術的説明はPRの記述と整合し、因果関係も正しく示されている。

事実の突合 ✓ PASS

PR情報による主張の裏付け(ハルシネーション検出)

全ての主張はPRのタイトル、Description、Diffで裏付けられており、ハルシネーションは見当たらない。

数値・固有名詞の確認 ✓ PASS

PR番号・コミットID・バージョン等の正確性

PR番号 #57808 が正しく記載されている。その他数値的記述はなし。

タイトル・説明との一致 ✓ PASS

記事タイトル・説明とPR内容の一致

記事タイトルはPRタイトルと完全に一致している。

外部知識の正確性 ✓ PASS

PRに記載のない外部知識(LTS、サポート状況など)の不使用

記事はPRに記載された範囲のみで説明しており、外部のバージョン情報やリリース計画等は含まれていない。

時間表現の正確性 ✓ PASS

時間表現がPR情報と一致しているか

時間表現の歪曲はなく、PRの記述と一致している。