Ruby on Rails:更新到破坏我模型预先存在的属性、选项的 gem?
我一直在使用的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对 PaperTrail 不太熟悉(尽管我一直想研究一下)。假设 PaperTrail 没有配置选项来更改 *version_name* 的名称,您可能可以在模型中以这种方式解决它:
只要您想访问您的属性,就使用 *version_name_attr* ,并在需要时使用 *verson_name* PaperTrail 方法。
这样的东西更干净,但如果 PaperTrail 在内部使用 *version_name* ,可能会破坏一些东西。
在这种情况下,当您需要 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:
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.
In this case, use *paper_trail_version_name* when you want the PaperTrail method. Access to your attribute would remain as you expect it.