saved_change_to_ *在Rails 5.2中使用“ activerecord-typedstore” gem时无法正常工作

问题描述 投票:1回答:1

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

我还没有找到自己解决问题的方法,所以我在宝石的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行为?或者也许我可以替代使用其他宝石?

ruby-on-rails activerecord ruby-on-rails-5 ruby-on-rails-5.2
1个回答
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

结果:

firstsecond

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

© www.soinside.com 2019 - 2024. All rights reserved.