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

different behaviors between saved_changes and changes #30508

Closed
rita1014 opened this Issue Sep 2, 2017 · 4 comments

Comments

Projects
None yet
4 participants
@rita1014

rita1014 commented Sep 2, 2017

I'm migrating my old rails project, because of the deprecation warning. but I find that the results of saved_changes & changes are different on virtual fields.

# my model
class Model
    attribute :virtual_column
    def virtual_column
    end
end
m.attribute_will_change! :virtual_column

in after_update. I can see virtual_column in changes, but cannot find it in saved_changes

@composerinteralia

This comment has been minimized.

Show comment
Hide comment
@composerinteralia
Member

composerinteralia commented Sep 2, 2017

@chenkovsky

This comment has been minimized.

Show comment
Hide comment
@chenkovsky

chenkovsky Sep 3, 2017

@composerinteralia

# frozen_string_literal: true

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"
  # Activate the gem you are reporting the issue against.
  gem "activerecord", "5.1.0"
  gem "sqlite3"
end

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

# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
    t.integer :status, default: 0
  end
end

class Post < ActiveRecord::Base
  attribute :post_valid
  def post_valid
    status >= 1
  end
  alias_method :old_write_attribute, :write_attribute

  def write_attribute(key, val)
    if key == "status"
      attribute_will_change! :post_valid
    end
    old_write_attribute(key, val)
  end

  attr_accessor :saved_changes_dump, :changes_dump
  after_update do
    self.saved_changes_dump = saved_changes
    self.changes_dump = changes
  end
end

class BugTest < Minitest::Test
  def test_association_stuff
    post = Post.create!
    post.status = 1
    post.save!
    STDERR.puts "saved_changes_dump: #{post.saved_changes_dump}"
    STDERR.puts "changes_dump: #{post.changes_dump}"
    assert_equal post.saved_changes_dump, post.changes_dump
  end
end

chenkovsky commented Sep 3, 2017

@composerinteralia

# frozen_string_literal: true

begin
  require "bundler/inline"
rescue LoadError => e
  $stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
  raise e
end

gemfile(true) do
  source "https://rubygems.org"
  # Activate the gem you are reporting the issue against.
  gem "activerecord", "5.1.0"
  gem "sqlite3"
end

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

# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)

# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
    t.integer :status, default: 0
  end
end

class Post < ActiveRecord::Base
  attribute :post_valid
  def post_valid
    status >= 1
  end
  alias_method :old_write_attribute, :write_attribute

  def write_attribute(key, val)
    if key == "status"
      attribute_will_change! :post_valid
    end
    old_write_attribute(key, val)
  end

  attr_accessor :saved_changes_dump, :changes_dump
  after_update do
    self.saved_changes_dump = saved_changes
    self.changes_dump = changes
  end
end

class BugTest < Minitest::Test
  def test_association_stuff
    post = Post.create!
    post.status = 1
    post.save!
    STDERR.puts "saved_changes_dump: #{post.saved_changes_dump}"
    STDERR.puts "changes_dump: #{post.changes_dump}"
    assert_equal post.saved_changes_dump, post.changes_dump
  end
end
@michelson

This comment has been minimized.

Show comment
Hide comment
@michelson

michelson Oct 10, 2017

hi @chenkovsky

use changes_include?(attr_name)

michelson commented Oct 10, 2017

hi @chenkovsky

use changes_include?(attr_name)

@rails-bot rails-bot bot added the stale label Jan 9, 2018

@rails-bot

This comment has been minimized.

Show comment
Hide comment
@rails-bot

rails-bot bot Jan 9, 2018

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 5-1-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

rails-bot bot commented Jan 9, 2018

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 5-1-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@rails-bot rails-bot bot closed this Jan 16, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment