Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reify tries to set the value for a read_only attribute #1467

Open
3 tasks done
professor opened this issue Apr 26, 2024 · 1 comment · May be fixed by #1468
Open
3 tasks done

reify tries to set the value for a read_only attribute #1467

professor opened this issue Apr 26, 2024 · 1 comment · May be fixed by #1468

Comments

@professor
Copy link

professor commented Apr 26, 2024

Rails 7.1 modifies raise_on_assign_to_attr_readonly link to be enabled by default. In the past, the code would silently carry on. I'm now fixing our code base to no longer write to a read_only attribute.

Reify sets a model's readonly attribute here:

    if model.has_attribute?(k)
          model[k.to_sym] = v

Check the following boxes:

  • This is not a usage question, this is a bug report
  • This bug can be reproduced with the script I provide below
  • This bug can be reproduced in the latest release of the paper_trail gem

Bug reports must use the following template:

# frozen_string_literal: true

# Use this template to report PaperTrail bugs.
# Please include only the minimum code necessary to reproduce your issue.
require "bundler/inline"

# STEP ONE: What versions are you using?
gemfile(true) do
  ruby "3.3.1"
  source "https://rubygems.org"
  gem "activerecord", "7.1.3.2"
  gem "minitest", "5.21.2"
  gem "paper_trail", "15.1.0", require: false
  gem "sqlite3", "1.5"
end

require "active_record"
require "minitest/autorun"
require "logger"

ActiveRecord.raise_on_assign_to_attr_readonly = true
ActiveRecord.use_yaml_unsafe_load = true

# Please use sqlite for your bug reports, if possible.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = nil
ActiveRecord::Schema.define do
  # STEP TWO: Define your tables here.
  create_table :users, force: true do |t|
    t.text :first_name, null: false
    t.timestamps null: false
  end

  create_table :versions do |t|
    t.string :item_type, null: false
    t.integer :item_id, null: false
    t.string :event, null: false
    t.string :whodunnit
    t.text :object, limit: 1_073_741_823
    t.text :object_changes, limit: 1_073_741_823
    t.datetime :created_at
  end
  add_index :versions, %i[item_type item_id]
end
ActiveRecord::Base.logger = Logger.new(STDOUT)
require "paper_trail"

# STEP FOUR: Define your AR models here.
class User < ActiveRecord::Base
  has_paper_trail
  attr_readonly :id
end

# STEP FIVE: Please write a test that demonstrates your issue.
class BugTest < ActiveSupport::TestCase
  def test_1
    user = User.create(first_name: "Jane")
    user.update! first_name: "Janet"

    versions = user.versions
    old_user = versions.last.reify
  end
end

# STEP SIX: Run this script using `ruby my_bug_report.rb`

Here is the output that I see....

BugTest#test_1:
ActiveRecord::ReadonlyAttributeError: id
@professor
Copy link
Author

I plan to put up a bug fix early next week.

@professor professor linked a pull request Apr 29, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant