【问题标题】:saved_change_to_* does't work correctly when using "activerecord-typedstore" gem with Rails 5.2在 Rails 5.2 中使用“activerecord-typedstore”gem 时,saved_change_to_* 无法正常工作
【发布时间】:2019-10-02 23:44:15
【问题描述】:

我们在 Rails 5.2 应用程序中使用 activerecord-typedstore gem。除了 after 回调中的“脏属性”方法外,一切都很好。根据 gem 的页面,它应该适用于 Rails 5.2,但有一种简单的方法可以重现该问题

我自己没有找到解决方法,所以我在 gem 的 github 上发布了一个问题:https://github.com/byroot/activerecord-typedstore/issues/78

这个例子演示了这个问题:

class Profile < ApplicationRecord
  typed_store :properties do |p|
    p.string :phone
  end

  after_save :debug
  def debug
    puts "saved_change_to_phone #{saved_change_to_phone.inspect}"
  end
end

p = Profile.create(phone: "123")
p.save
p = Profile.last
p.phone = "456"
p.save

# Displays:
# saved_change_to_phone nil

是否可以在 after 回调中获得标准 Rails 5.2 存储属性的行为?或者也许我可以使用另一种 gem?

【问题讨论】:

    标签: ruby-on-rails activerecord ruby-on-rails-5 ruby-on-rails-5.2


    【解决方案1】:

    我已经测试了你的例子:

    我能够让它工作的唯一方法是像这样禁用访问器:

    # frozen_string_literal: true
    
    class Profile < ApplicationRecord
      validates :phone, presence: true
      typed_store :properties, accessors: false do |p|
        p.string :phone
      end
    
      after_save :debug
      def debug
        puts "saved_change_to_phone #{saved_change_to_phone.inspect}"
      end
    end
    

    结果:

    我不知道这是否是您想要的行为。

    【讨论】:

    • 有趣的结果。谢谢@mutantkeyboard
    猜你喜欢
    • 2018-06-19
    • 1970-01-01
    • 2016-07-18
    • 2016-04-11
    • 1970-01-01
    • 2016-12-05
    • 2013-11-12
    • 1970-01-01
    • 2019-03-16
    相关资源
    最近更新 更多