will_paginateとaction_web_servicesを組み合わせた際のmethod_missingエラーについて
Rails2.0になった際に、ページネーションとAction Web Serviceがそれぞれプラグインで提供されるようになりました。Action Web Serviceは
script/plugin install http://biorails.org/svn/biorails/plugins/action_web_services
にてインストールできます。で、この二つを組み合わせた場合に、なぜかモデル内に定義した関数が使えなくなりました。
NoMethodError (undefined method `authenticate' for #<Class:0x2360a04>):
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb:1532:in `method_missing_without_paginate'
/vendor/plugins/will_paginate/lib/will_paginate/finder.rb:101:in `method_missing'
このような感じです。なぜかClassに関数がないといってきます。実際に処理を呼び出しているのはUserモデルのUser. authenticateで、
@user=User.authenticate(h[:username], h[:password])
のように呼び出しています。が、メソッドがないと言われます。実際、コンソールでやっても失敗します。
原因は不明なのですが、
vendor/plugins/will_paginate/lib/will_paginate/finder.rb
def method_missing_with_paginate(method, *args, &block)
# did somebody tried to paginate? if not, let them be
unless method.to_s.index('paginate') == 0
return method_missing_without_paginate(method, *args, &block)
end
ここに処理が飛んで、当然paginateを使っていないのでmethod_missing_without_paginateに飛ばされ、
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/base.rb
def method_missing(method_id, *arguments)
内の
send(method_id, *arguments)
else
super
end
にてsuperが呼び出されてエラーになっているようです。回避策としては、
def authenticate(name, passwd) (処理内容) end
↓
def self.authenticate(name, passwd) (処理内容) end
のようにself.を付けるとエラーが出なくなります。