在Rails模型中设置'published_on'日期字段

时间:2012-05-31 15:19:41

标签: ruby-on-rails ruby

这是我玩Ruby on Rails的第二天,所以请保持温和:)

我有一个非常(希望)简单的问题:我想在我的模型上设置一个发布日期,当它确实被创建时。我正在使用Mongoid,它看起来像这样:

class Page
  include Mongoid::Document

  field :content, type: String
  field :title, type: String
  field :published_on, type: DateTime
end

以下是问题:我应该在调用published_on之前设置create操作中的@page.save字段吗?还是有更好的,更惯用的方式?

如果我这样做,意思是,在控制器的操作中调用@page.published_on = Date.now,我会收到警告:

  

找不到类型'Page'

的'published_on ='

1 个答案:

答案 0 :(得分:3)

如果你把

class Page
  include Mongoid::Document
  include Mongoid::Timestamps

您获得了rails自动化字段created_atupdated_at