覆盖 Rails 3 中的范围
我想覆盖现有范围以向其添加额外条件。我已经尝试使用 alias_method 来执行此操作。不幸的是,这种方法不适用于范围,我收到未定义的方法错误。我该如何使用示波器来做到这一点?
module Delayed
module Backend
module ActiveRecord
class Job < ::ActiveRecord::Base
belongs_to :queue
scope :in_unlocked_queue, lambda {
joins(:queue) & Queue.unlocked
}
alias_method :orig_ready_to_run, :ready_to_run
scope :ready_to_run, lambda {|worker_name, max_run_time|
orig_ready_to_run(worker_name, max_run_time).in_unlocked_queue
}
end
end
end
end
I want to override an existing scope to add an extra condition to it. I've shown my attempt to do this using alias_method. Unfortunately this approach doesn't work with scopes, I get an undefined method error. How do I do it with scopes?
module Delayed
module Backend
module ActiveRecord
class Job < ::ActiveRecord::Base
belongs_to :queue
scope :in_unlocked_queue, lambda {
joins(:queue) & Queue.unlocked
}
alias_method :orig_ready_to_run, :ready_to_run
scope :ready_to_run, lambda {|worker_name, max_run_time|
orig_ready_to_run(worker_name, max_run_time).in_unlocked_queue
}
end
end
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,这是一个答案,不确定它是否是最干净的,但它有效
OK, here's an answer, not sure if it's the cleanest but it works