[英]Can't override class method with monkey patching in Ruby on Rails
所以我试图覆盖 ActiveRecord::Reflection
的类方法 reflect_on_association
。 这是原始文件的链接: https : //github.com/rails/rails/blob/master/activerecord/lib/active_record/reflection.rb
该方法在第106行中定义。
到目前为止,这是我的尝试:
1。
ActiveRecord::Reflection::ClassMethods.module_eval do
# A test method
def say_hello
puts 'hello'
end
# I want to override the original method with this one
def reflect_on_association(association)
puts 'overridden!'
# < Implementation goes here >
end
end
2。
module ActiveRecord::Reflection::ClassMethods
# A test method
def say_hello
puts 'hello'
end
# I want to override the original method with this one
def reflect_on_association(association)
puts 'overridden!'
# < Implementation goes here >
end
end
该say_hello
方法适用于这两种情况下(例如,当我打电话Person.say_hello
),但仍然没有运气reflect_on_association
。
有人对我该怎么做有任何想法吗? 非常感谢你!
在可能的情况下工作!
确保您使用reflect_on_association传递参数。
1.9.3-p551 :514 > Person.reflect_on_association(state)
overridden!
=> nil
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.