简体   繁体   English

Ruby on Rails:更新为破坏我模型的现有属性的宝石吗?

[英]Ruby on Rails: Update to a gem clobbering my model's pre-existing attribute, options?

A gem I've been using adds methods to models. 我一直在使用的gem在模型中添加了方法。 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? 除了重命名数据库中的列并更新我的所有代码之外,是否有其他解决方法,如果我希望保持最新状态,可以使用gem?

In case it is helpful, to make this more concrete, the gem is PaperTrail, which adds version tracking to models. 为了使它更具体,如果有用,gem是PaperTrail,它会向模型添加版本跟踪。 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. 我的模型在数据库中有一个预先存在的属性,称为ve​​rsion_name,该属性刚刚被添加为PaperTrail的最新版本,作为class_attribute version_name,供PaperTrail用来定义另一种方法的名称。

Not that familiar with PaperTrail (though I've been meaning to look into it). 对PaperTrail不太熟悉(尽管我一直想研究它)。 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: 假设PaperTrail没有配置选项来更改* version_name *的名称,那么您可能会在模型中以这种方式解决它:

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. 每当您要访问属性时,只需使用* version_name_attr *;在需要PaperTrail方法时,请使用* verson_name *。

Something like this is a bit cleaner , but might break things if PaperTrail uses *version_name* internally. 像这样的东西更干净一些 ,但是如果PaperTrail内部使用* version_name *,可能会破坏事情。

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. 在这种情况下,当需要PaperTrail方法时,请使用* paper_trail_version_name *。 Access to your attribute would remain as you expect it. 对属性的访问将保持您期望的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 带有新Rails应用程序的现有数据库 - Pre-existing database with new rails application Ruby on Rails使用预先存在的数据库。 Rails使一切变得复数 - Ruby on Rails working with pre-existing database. Rails makes everything plural Ruby on Rails Monkey修补Gem的模型 - Ruby on Rails Monkey Patching a Gem's Model 通过systemd套接字激活来启动Ruby on Rails应用程序(即,启动Rails服务器监听预先存在的套接字) - Start Ruby on Rails app through systemd socket activation (i.e. start Rails server listening on pre-existing socket) 如何在Ruby on Rails中从Gem向现有模型添加多态关联? - How to add a polymorphic association to an existing model from a Gem in Ruby on Rails? Rails表单-检查预先存在的数据并填充字段 - Rails Form - Check for pre-existing data and populate fields Rails Select:当“选择”中没有“现有记录”时 - Rails Select: When Pre-existing Record is not in Select 使用现有记录在Rails中向数据库添加新记录 - Using pre-existing records to add a new record to the database in rails 生成一个使用数据库中现有数据的模型 - Generate a model that uses pre-existing data from database 使用回形针将对象与S3上的现有文件相关联 - Associate object to pre-existing file on S3, using Paperclip
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM