2012-05-08 4 views
1

私は、モンキー・パッチを作成して、created_atのメソッドを追加しようとしています。モンキー・パッチfor 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

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

を試してみましたあなたはclass Timeでそれを宣言しようとしましたか?

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

TimeWithZoneTimeないDateTimeに委譲し、その時のオブジェクトのように見えます。

またTimeWithZoneあなたは

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

ような何かをしなければならないので、ちょうど@timeオブジェクトを超えていしかし、私はそのコードの100%わかりません。