2012-05-08 2 views
1

created_at에 대한 메소드를 추가하기 위해 원숭이 패치를 작성하려고합니다. created_at에 대한 원숭이 패치

은 내가 date_time_extras.rb 파일을 생성하고 내용과 함께 lib 디렉토리에 넣어 :

class DateTime 
    def beginning_of_hour 
    change(:min => 0) 
    end 
end 

을 내가 콘솔에서 :

record.created_at.beginning_of_hour 

하지만이 방법은없는 오류를 얻을 수 있습니다. created_at가 datetime이 아닌 것 같습니다. DateTime.new.beginning_of_hour이 작동하기 때문에 record.created_at.classActiveSupport::TimeWithZone을 산출합니다.

그래서 created_at 유형의 날짜에 원숭이 패치를 작성하려면 어떻게해야합니까?

레일즈 3.0.10 버전을 사용하고 있습니다.

업데이트

또한

+0

Welp .. 중간에서 'record.created_at.change (: min => 0)'을 사용할 수있는 것처럼 보입니다. – CambridgeMike

답변

0

당신이 그것을 class Time에 선언 시도해 봤어 아무 소용

module ActiveSupport 
    class TimeWithZone 
    def beginning_of_hour 
     change(:min => 0) 
    end 
    end 
end 

를 시도?

class DateTime 
    def beginning_of_hour 
    change(:min => 0) 
    end 
end 

TimeWithZoneTime하지 DateTime에 위임의 시간 객체처럼 보인다. 당신이

module ActiveSupport 
    class TimeWithZone 
    def beginning_of_hour 
     self.time.change(:min => 0) 
    end 
    end 
end 

처럼 뭔가를해야하지만 그 코드에 확실히 100 % 아니에요, 그래서

또한 TimeWithZone 단지 @time 객체보다 더 들어 있습니다.

관련 문제