Ruby类方法不起作用

时间:2012-11-12 19:54:24

标签: ruby-on-rails-3

class Post < ActiveRecord::Base
 belongs_to :users

 def self.find_latest_closed
   Post.where("status=?",'Closed').order("updated_at DESC").limit(2)
 end

 def self.find_latest_open
   Post.where("status=?",'Open').order("updated_at DESC").limit(2)
 end
end

在PostController中

class UsersController < ApplicationController
 def dashboard
  @totalclose = Post.find_latest_closed
   @totalopen = Post.find_latest_open
 end
end

我收到以下错误

NoMethodError in UsersController#dashboard
undefined method `find_latest_closed' for #<Class:0x000001074eb760>

这里有什么问题?

2 个答案:

答案 0 :(得分:0)

我会尝试删除方法调用的Post前缀。

 def self.find_latest_closed
   where("status=?",'Closed').order("updated_at DESC").limit(2)
 end

 def self.find_latest_open
   where("status=?",'Open').order("updated_at DESC").limit(2)
 end

答案 1 :(得分:0)

你可能与ActiveRecord的魔法find_*方法存在某种冲突。也许尝试将方法名称更改为get_*