method_missing, , , . - , - x.boo boo , method_missing boo, boo () :
class ActiveRecord::Base
def method_missing(meth, *args, &block)
if meth.to_s =~ /^find_by_(.+)$/
run_find_by_method($1, *args, &block)
else
super
end
end
def run_find_by_method(attrs, *args, &block)
attrs = attrs.split('_and_')
attrs_with_args = [attrs, args].transpose
conditions = Hash[attrs_with_args]
where(conditions).all
end
end
define_method , , , method_missing. :
%w(user email food).each do |meth|
define_method(meth) { @data[meth.to_sym] }
end