0
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>

这里有什么问题?

4

2 回答 2

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
于 2012-11-12T20:25:56.743 回答
0

您可能与 ActiveRecord 的魔术find_*方法有某种冲突。也许尝试将方法名称更改为get_*.

于 2012-11-12T20:37:55.067 回答