Rails row locking and stale-update recipes
Choose the Rails implementation for the write conflict you need to prevent: serialize stock reservations under a row lock or reject a cancellation based on a stale version. Each recipe links its saved requirement and captured broken/fixed source checks.
Download the Rails rule pack ZIP · Inspect the YAML rules
Ruby
The lock reloads the record and serializes competing reservations. An availability check made before taking the lock can become stale. Keeping the check, decrement, and save inside with_lock makes the inventory decision atomic for that row.
Broken edit flagged · corrected edit passes
Ruby
Active Record detects a write conflict after the record is loaded, but it cannot infer that a customer made a decision using an older screen. Compare the submitted version first, then let lock_version protect the save from a race that occurs during this request.
Broken edit flagged · corrected edit passes
Introductory framework example
Use a correction as a saved requirement
Choose the failure you need to prevent, add the scoped rule to your repository, and check both the broken and corrected implementation. Ask your agent to inspect the finding, repair the code and recheck the same rule. Keep framework tests beside the model check.
Perch keeps the requirement beside the code. Reuse the named check during the agent’s next edit and in CI, with cached answers free on unchanged requests.
Official Rails documentation · All language and framework recipes
Inspect the edit and the saved check
The Serialize Rails stock reservations with an Active Record row lock example checks app/services/reserve_stock.rb. The source below is the same example used by its linked capture.
Compare the broken edit with the corrected implementation
Broken implementation
class ReserveStock
def self.call(inventory, quantity)
raise ArgumentError, "positive integer required" unless quantity.is_a?(Integer) && quantity > 0
inventory.with_lock do
inventory.available -= quantity
inventory.save!
end
inventory.available
end
end
Corrected implementation
class ReserveStock
def self.call(inventory, quantity)
raise ArgumentError, "positive integer required" unless quantity.is_a?(Integer) && quantity > 0
inventory.with_lock do
raise "insufficient stock" if inventory.available < quantity
inventory.available -= quantity
inventory.save!
end
inventory.available
end
end
Exact source and edit · Raw captured check
Make the agent check the requirement after its edit
Keep your existing perch.yaml. The scoped rule pack installs the saved requirement under .perch/rules/. Review its file selector and use the implementation’s integration prerequisites before adapting it to your application.
npm install -g @lakeday/perch
perch login
# Choose the setup command for your agent:
perch setup claude-code
# Or: perch setup codex
# Or: perch setup cursor
Check the edit and recheck the repair
curl -fL 'https://reporules.fyi/rails/locked-stock/rule-pack.zip' -o requirement.zip
unzip -n requirement.zip
perch check app/services/reserve_stock.rb --rules locked-stock-availability --json
# Read the finding, repair the implementation, then repeat:
perch check app/services/reserve_stock.rb --rules locked-stock-availability --json
Run the same check on the repaired working file before committing. Keep the application regression tests alongside it. Fresh model judgments can vary; an unchanged request can reuse its cached answer. A passing check does not prove application correctness.