【发布时间】:2013-06-24 18:16:00
【问题描述】:
我在 Rails 中动态定义类方法如下:
class << self
%w[school1 school2].each do |school|
define_method("self.find_by_#{school}_id") do |id|
MyClass.find_by(school: school, id: id)
end
end
end
如何使用缺少的方法调用find_by_SOME_SCHOOL_id 而无需在%w[school1 school2] 中预先定义这些学校?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 method-missing