2012-11-12 12 views
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 

를 작동하지 않는 나는 여기에 다음과 같은 오류가루비 클래스 메소드는 <pre><code>class UsersController < ApplicationController def dashboard @totalclose = Post.find_latest_closed @totalopen = Post.find_latest_open end end </code></pre> <p>PostController</p>에서

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

무슨 오해?

+2

콘솔에서 작동합니까? models/post.rb의 클래스 정의입니까? 또한 맞춤법을 검사하십시오 -이 오류는 일반적으로 오타가 원인입니다 ... – PinnyM

+2

클래스 메소드 대신 scope를 사용해야합니다 (예 : scope : two_latest_open, where (status : 'Open'). order ('updated_at DESC'). limit (2)' – MrYoshiji

+1

여기에 무슨 일이 일어나고 있는지에 관계없이 자신의 메서드를 정의하는 대신에'''scope''를 사용하는 것이 더 나을 것입니다. ": scope : latest_closed, where ('status =?', ' –

답변

0

그것은 당신이 액티브의 마법 find_* 방법과 충돌의 일종있어 가능합니다. 어쩌면 메서드 이름을 get_*으로 변경해보십시오.

0

나는 메서드 호출의 접두사를 제거하려고 할 것이다.

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 
관련 문제