各ユーザーには多くの役割があります。ユーザーが「管理者」ロールを持っているかどうかを確認するには、次のhas_role?
メソッドを使用できます。
some_user.has_role?('admin')
これは次のように定義されています。
def has_role?(role_in_question)
roles.map(&:name).include?(role_in_question.to_s)
end
some_user.has_role?('admin')
のように書けるようにしたいsome_user.is_admin?
ので、次のようにしました。
def method_missing(method, *args)
if method.to_s.match(/^is_(\w+)[?]$/)
has_role? $1
else
super
end
end
このsome_user.is_admin?
場合は問題なく動作しますが、別の関連付けで参照されているユーザーで呼び出そうとすると失敗します。
>> Annotation.first.created_by.is_admin?
NoMethodError: undefined method `is_admin?' for "KKadue":User
from /Library/Ruby/Gems/1.8/gems/activerecord-2.3.4/lib/active_record/associations/association_proxy.rb:215:in `method_missing'
from (irb):345
from :0
何を与える?