开发者

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. 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 concr开发者_如何转开发ete, 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜