[英]Can't override class method with monkey patching in Ruby on Rails
So I am trying to override the class method reflect_on_association
in ActiveRecord::Reflection
. 所以我试图覆盖
ActiveRecord::Reflection
的类方法 reflect_on_association
。 Here's the link to the original file: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/reflection.rb 这是原始文件的链接: https : //github.com/rails/rails/blob/master/activerecord/lib/active_record/reflection.rb
The method was defined on line 106. 该方法在第106行中定义。
These are my attempts so far: 到目前为止,这是我的尝试:
1. 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. 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
The say_hello
methods works for both cases (for example when I call Person.say_hello
), but still no luck with reflect_on_association
. 该
say_hello
方法适用于这两种情况下(例如,当我打电话Person.say_hello
),但仍然没有运气reflect_on_association
。
Anyone has any idea on how I can do this? 有人对我该怎么做有任何想法吗? Thank you very much!
非常感谢你!
working in may case! 在可能的情况下工作!
make sure you are passing parameter with 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.