想用
method_missing构造动态方法,以"can_" 开头
我跟踪后发现不执行此方法
ruby 代码
- class User < ActiveRecord::Base
- def has_role?(role_in_question)
- @_list ||= self.roles.collect(&:name)
- return true if @_list.include?("admin")
- (@_list.include?(role_in_question.to_s) )
- end
- def method_missing(method_id,arg)
- method_name = method_id.id2name
- arr = method_name.split("_")
- if arr[0] == "can"
- has_role?(arg)
- else
- raise NoMethodError,"there is no method:#{method_name}"
- end
- end
- end