Ruby on Rails:更新到破坏我模型预先存在的属性、选项的 gem?

发布于 2024-12-07 04:33:33 字数 300 浏览 6 评论 0原文

我一直在使用的 gem 为模型添加了方法。它最近更新了其方法名称,使得方法名称之一现在与我的模型预先存在的数据库属性之一相同。

如果我希望保持最新的 gem,除了重命名数据库中的列并更新所有代码之外,还有其他解决方法吗?

如果有帮助的话,为了让这个更具体,最好的方法是 PaperTrail,它为模型添加了版本跟踪。我的模型在数据库中有一个名为 version_name 的预先存在的属性,最新版本的 PaperTrail 刚刚将其添加为 class_attribute version_name,PaperTrail 使用该属性来定义另一个方法的名称。

A gem I've been using adds methods to models. It recently updated its method names such that one of the method names is now the same as one of my model's pre-existing database attributes.

Are there any workarounds to this problem other than renaming the column in my database and updating all of my code if I wish to stay up-to-date with the gem?

In case it is helpful, to make this more concrete, the gem is PaperTrail, which adds version tracking to models. My model had a pre-existing attribute in the database called version_name, which the latest version of PaperTrail just added as a class_attribute version_name that is used by PaperTrail to define the name of another method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽袖吟 2024-12-14 04:33:33

对 PaperTrail 不太熟悉(尽管我一直想研究一下)。假设 PaperTrail 没有配置选项来更改 *version_name* 的名称,您可能可以在模型中以这种方式解决它:

class Thingy
  def version_name_attr
    attributes['version_name']
  end

  def version_name_attr=(val)
    attributes['version_name'] = val
  end
end

只要您想访问您的属性,就使用 *version_name_attr* ,并在需要时使用 *verson_name* PaperTrail 方法。

这样的东西更干净,但如果 PaperTrail 在内部使用 *version_name* ,可能会破坏一些东西。

class Thingy
  alias_method :paper_trail_version_name, :version_name
  def version_name
    attributes['version_name']
  end
end

在这种情况下,当您需要 PaperTrail 方法时,请使用 *paper_trail_version_name*。对您的属性的访问将保持如您所期望的那样。

Not that familiar with PaperTrail (though I've been meaning to look into it). Assuming PaperTrail doesn't have a config option to change the name of *version_name*, you could probably get around it this way in your model:

class Thingy
  def version_name_attr
    attributes['version_name']
  end

  def version_name_attr=(val)
    attributes['version_name'] = val
  end
end

Just use *version_name_attr* whenever you want to access your attribute, and *verson_name* when you want the PaperTrail method.

Something like this is a bit cleaner, but might break things if PaperTrail uses *version_name* internally.

class Thingy
  alias_method :paper_trail_version_name, :version_name
  def version_name
    attributes['version_name']
  end
end

In this case, use *paper_trail_version_name* when you want the PaperTrail method. Access to your attribute would remain as you expect it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文