← Back to Pull requests

Fix tracked column writes on Rails main #24

Closed
unknown closed this pull request without merging on Nov 02, 2026 16:07 UTC
View on GitHub
unknown unknown commented on Oct 21, 2026 14:40 UTC
Author

We're now overriding define_method_attribute=, which is a method defined in
Rails to generate setter methods for models' columns. If we want to track
changes to a column, we need the setter to call _write_attribute, as it used
to, because that's the method DeltaChanges overrides to track changes.
Otherwise, Rails' default generated setter will call
@attributes.write_from_user directly, thus bypassing our override/"hook" into
_write_attribute. Our setter still ends up calling
@attributes.write_from_user, but it gets there through _write_attribute,
which allows DeltaChanges to record the change.

By overriding define_method_attribute=, we can generate the setter we need,
but we only want to do this on Rails versions which no longer generate setters
that call _write_attribute, and only for columns DeltaChanges has been
configured to track. Currently, that's Rails main, which our version check
includes using >= 8.2.a. If we're on an older version of Rails which already
generates setters that call _write_attribute, our DeltaChanges "hook" already
works, so we can skip defining our generator override entirely. On newer Rails,
if DeltaChanges hasn't been configured to track a particular column, we can
call super to allow Rails' default setter generator to handle that column.

There's also the case where Rails has already generated setter methods before
DeltaChanges is configured. Setting the configuration won't change those
existing methods on its own. On these newer Rails versions, calling
delta_changes with a nonempty :columns list also calls
undefine_attribute_methods. This removes the generated attribute methods and
resets Rails' "attribute methods generated" flag. The next time Rails calls
define_attribute_methods, for example when initializing a record, our generator
override uses the newly configured options to decide which setters should call
_write_attribute and which can be generated by Rails when our override calls
super.

Ref: https://github.com/rails/rails/commit/0f6c0a05e2064b56f759677d94f060f057d89c66

Something worth mentioning, by routing through _write_attribute, we're essentially undoing the benefits introduced by doing direct writes instead. However, benchmarks (adapted from https://github.com/rails/rails/pull/59191) show there is no performance regression of DeltaChanges between Rails 8.1 and Rails 8.2+. _When benchmarking between tracked and untracked columns in Rails 8.2+ (not shown), we do see DeltaChanges' overhead increase._

<details><summary>Benchmark</summary>
<p>

require "bundler/inline"

rails_version = ENV.fetch("RAILS_VERSION", "8.1")

gemfile(true) do
  source "https://rubygems.org"

  case rails_version
  when "8.1"
    gem "activerecord", "= 8.1.4"
  when "8.2"
    gem "activerecord", github: "rails/rails", branch: "main"
  else
    raise "RAILS_VERSION must be 8.1 or 8.2"
  end

  gem "sqlite3"
  gem "benchmark-ips"
  gem "delta_changes", path: File.expand_path("..", __dir__)
end

require "active_record"
require "benchmark/ips"
require "delta_changes"

if ActiveRecord.version < Gem::Version.new("8.2.a")
  ActiveRecord.raise_on_assign_to_attr_readonly = true
end

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Schema.verbose = false
ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
    t.string :title, null: false
    t.string :body
    t.string :type_name, null: false
    t.string :key, null: false
    t.integer :upvotes, null: false
    t.integer :author_id, null: false
    t.timestamps
  end
end

class Post < ActiveRecord::Base
  include DeltaChanges::Extension
  attr_readonly :key
  delta_changes columns: %w[title body type_name upvotes], attributes: []
end

post = Post.create!(
  title: Random.alphanumeric(30),
  type_name: Random.alphanumeric(10),
  key: Random.alphanumeric(10),
  body: Random.alphanumeric(100),
  upvotes: 0, # Ensure the timed assignment to 12 always creates a tracked change.
  author_id: rand(30),
)
post.reset_delta_changes!

Benchmark.ips do |x|
  x.report("delta_changes-#{ActiveRecord.version}") do
    post.title = "title 1"
    post.title = "title 2"
    post.title = "title 3"
    post.body = "body"
    post.type_name = "type"
    post.upvotes = 12
  end
  x.compare!(order: :baseline)
  x.save!(File.join(__dir__, "benchmark_pr_59191.data"))
end

</p>
</details>

Warming up --------------------------------------
delta_changes-8.2.0.alpha    14.615k i/100ms
Calculating -------------------------------------
delta_changes-8.2.0.alpha    148.618k (± 0.7%) i/s    (6.73 μs/i) -    745.365k in   5.015322s

Comparison:
      delta_changes-8.1.4:   137524.8 i/s
delta_changes-8.2.0.alpha:   148617.6 i/s - 1.08x  faster

Diff: 612 lines (+459 / -153) across 7 files Reviewer: grosser Review duration: 17366m
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 612 lines diff across 7 files
0.50
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
0.17
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
60%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.384
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #24"
general weight: 0.35
Reviewers
Assignees
Unassigned
Labels
risk: low confidence: 60.5% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 50.2% (MEDIUM) · Higher = larger blast radius
50.2%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 60.5% · Lower = rushed or shallow review
60.5%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 19.8% · Safe to merge
19.8%