Model.find_by_name などでも before_find したい場合には,
class Human < ActiveRecord::Base
self.abstract_class = true
class << self
def find(*params)
puts "before_find"
super
end
def method_missing(method_id, *arguments)
if match = /^find_(all_by|by)_([_a-zA-Z]\w*)$/.match(method_id.to_s)
# find_* なので before_find 範疇
puts "Human.before_find_by_*"
end
super
end
end
end
class Person < Human
end
などとする.これもちょっと強引だなぁ.
comments powered by Disqus