[英]RoR: how to get the creation timestamp in the model
I have a class Event where I need a method that compares the current date to the date the Event was created. 我有一个Event类,在其中需要一个将当前日期与Event创建日期进行比较的方法。 Can you tell me how to access the created_at timestamp the scaffold created for the Event database table? 您能告诉我如何访问为事件数据库表创建的支架的created_at时间戳吗?
class Event < ActiveRecord::Base
attr_accessible :location, :name
def isInsertedLongAgo
# current date - insertion date >= 90 days
end
end
Same as any other attribute - just call it: 与其他任何属性相同-只需调用它即可:
def isInsertedLongAgo
# current date - insertion date >= 90 days
Time.now - created_at >= 90.days
end
Hi you can acces created at using created_at field self.created_at
, At the time of scaffold time stamp is created which create created_at,updated_at You can directly access those variables. 嗨,您可以使用created_at字段self.created_at
创建的权限,在创建脚手架时创建create_at,updated_at时间戳。您可以直接访问这些变量。 replace insertion date with created_at 用created_at替换插入日期
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.