Timestamp not updated in RoR application

I have a small site that sorts news by the date it was sent to the second. Locally this works fine. Even with quick posting of stories in the line, there is a difference in the timestamp.

Example:

Submitted Fri Mar 25 14:31:09 2011 Submitted Fri Mar 25 14:30:45 2011 Submitted Fri Mar 25 14:30:23 2011 

However, as soon as the code is clicked on the hero and the database is configured with MongoHQ, the value of seconds seems ignored or frozen.

Example Document Value DateTime:

 added_on 03/14/2011 09:58 AM 

Example timestamps:

 Submitted Thu Mar 24 13:48:40 2011 Submitted Thu Mar 24 13:48:40 2011 Submitted Thu Mar 24 13:48:40 2011 Submitted Thu Mar 24 13:48:40 2011 

Seems like seconds value is not updating?

Here is the model code,

 class Post include Mongoid::Document field :link field :title field :synopsis field :added_on, :type => DateTime, :default => DateTime.now field :poster field :category validates_presence_of :link validates_presence_of :title validates_presence_of :synopsis validates_presence_of :category validates_uniqueness_of :link validates_uniqueness_of :title embeds_many :replies #referenced_in :topic end 
+4
source share
2 answers

I assume that you need to change the default value to Proc .

In development mode, your models are reloaded every request, so DateTime.now always current. However, during production, the class is loaded only once (on the dyno) during application startup, and DateTime.now results in a static value.

 field :added_on, :type => DateTime, :default => Proc.new { DateTime.now } 

should be what you want.

+6
source

This is an ad in your class:

 field :added_on, :type => DateTime, :default => DateTime.now 

Will be processed when reading a file. On Heroku, this happens when you click on your update and knock down the mucus. The result is that the default value is fixed at any time when the merge time is compiled. Try instead:

 field :added_on, :type => DateTime, :default => lambda { DateTime.now } 

Everything probably works fine in your development environment because the file is constantly reloading.

+5
source

Source: https://habr.com/ru/post/1346394/


All Articles