【发布时间】:2011-02-22 20:41:29
【问题描述】:
有没有办法覆盖 ActiveRecord 关联提供的方法之一?
例如,我有以下典型的多态 has_many :通过关联:
class Story < ActiveRecord::Base
has_many :taggings, :as => :taggable
has_many :tags, :through => :taggings, :order => :name
end
class Tag < ActiveRecord::Base
has_many :taggings, :dependent => :destroy
has_many :stories, :through => :taggings, :source => :taggable, :source_type => "Story"
end
您可能知道,这会为 Story 模型添加大量相关方法,例如标签、标签
如何覆盖这些方法之一?特别是 tags
def tags<< *new_tags
#do stuff
end
调用时会产生语法错误,所以显然没那么简单。
【问题讨论】:
-
你这样做是为了什么?这最终可能会破坏其他 ActiveRecord 功能,并且可能有更好的方法来做你正在尝试的事情
标签: ruby-on-rails ruby activerecord overriding