undefined method `strftime_format' - ruby-on-rails

I'm use gems stamp and StampI18n.
Code:
#post.time = #date.localize_stamp("1 Jan 1999, 21:45", :locale => 'en')
Result:
undefined method `strftime_format' for Thu, 09 Jun 2011:Date
What's the problem?

Related

How to join points by line?

I am using chartkick and gem "groupdate" in my project.
<%= area_chart SugarLevel.group_by_minute(created_at).sum(:mmol) %>
I want to keep continuing line from one point to another. I do something wrong with groupdate. If there is no data at some minute, the line has to start from previous point and has to finish at next point.
It has to be graph without gaps. Like that:
Method in gem "groupdate" group_by_something uses Hash to store a data. For example:
#hash = SugarLevel.group_by_minute(:created_at).sum(:mmol) # => {Thu, 01 Jun 2017 17:39:00 UTC +00:00=>#<BigDecimal:2add9a8,'0.77E1',18(18)>, Thu, 01 Jun 2017 17:40:00 UTC +00:00=>0, Thu, 01 Jun 2017 17:41:00 UTC +00:00=>0, Thu, 01 Jun 2017 17:42:00 UTC +00:00=>0, Thu, 01 Jun 2017 17:43:00 UTC +00:00=>0}
We can clear this hash as we want:
#hash.select{|k, v| v != 0} # Select all keys which do not consist 0
And eventually, we can put itin a view: <%= area_chart #hash %>

Comparison between two ActiveSupport::TimeWithZone objects fails

In my test suite, I have a failing test.
expected[0]['date'] comes from SomeModel.first.created_at
In a debugging console, I have the following:
> expected[0]['date']
=> Tue, 25 Mar 2014 16:01:45 UTC +00:00
> res[0]['date']
=> Tue, 25 Mar 2014 16:01:45 UTC +00:00
> res[0]['date'] == expected[0]['date']
=> false # wtf
> res[0]['date'].class
=> ActiveSupport::TimeWithZone
> expected[0]['date'].class
=> ActiveSupport::TimeWithZone
>
How is this possible ?
I've tried to reproduce this problem (I tought maybe the == operator on TimeWithZone checks the reference, or something like this, but no...) :
> t1 = Time.zone.at(0)
=> Thu, 01 Jan 1970 00:00:00 UTC +00:00
> t2 = Time.zone.parse(t1.to_s)
=> Thu, 01 Jan 1970 00:00:00 UTC +00:00
> t1 == t2
=> true
> t1.class
=> ActiveSupport::TimeWithZone
> t2.class
=> ActiveSupport::TimeWithZone
Edit: More tests...
> res[0]['date'].eql?(expected[0]['date'])
=> false
> res[0]['date'].zone
=> "UTC"
> expected[0]['date'].zone
=> "UTC"
> expected[0]['date'].getlocal
=> 2014-03-25 16:01:45 +0000
> res[0]['date'].getlocal
=> 2014-03-25 16:01:45 +0000
> res[0]['date'].hash
=> -3455877575500291788
> expected[0]['date'].hash
=> -3819233736262144515
>
> t1.hash
=> 2279159074362527997
> t2.hash
=> 2279159074362527997
# inspect...
> expected[0]['date'].inspect
=> "Tue, 25 Mar 2014 16:39:01 UTC +00:00"
> res[0]['date'].inspect
=> "Tue, 25 Mar 2014 16:39:01 UTC +00:00"
Looks like the comparison is based on the hash object. Why res and expected have different hashes ?
Answer #1 rake db:test:prepare
First, attempt dropping the test database and recreating it, and then running rake db:test:prepare. This has resolved this issue for me in the past I know this is a bit of a lame answer, but it is worth a shot.
Answer #2 Spring + Rspec + Shoulda matchers
If having this issue after installing Spring, please checkout this Github Thread, which can cause tests to fail:
https://github.com/rails/spring/issues/209
This issue only started occurring for me after adding Spring to my project.
Adding gem 'shoulda', require: false and manually adding require 'shoulda/matchers' to my spec_helper.rb resolved the issues
Answer #3 Timecop
If still having issues, checkout the Timecop gem and freeze time around date comparisons.
https://github.com/travisjeffery/timecop

How to set the timezone and get the time all in one line of code?

Time.zone = 'Hawaii' # => "Hawaii"
Time.zone.now # => Wed, 23 Jan 2008 20:24:27 HST -10:00
I would like to do something like Time.zone('Hawaii').now
Time.now.in_time_zone("Hawaii")

Strange Behavior with mongoid date range query. - Rails 3

I am running in very strange date range query problem. When I use DateTime.now.utc option to query database, the query date for DateTime.now is different that my current DateTime.now(i.e. one that's returned on console.)
My mogoid.yml uses-
use_utc: true
I have my named scope like this -
scope :running_auctions, {
:where => { :end_time.gt => DateTime.now.utc },
:order_by => [:end_time, "ASC"]
}
And on console I can do this -
Loading development environment (Rails 3.0.7)
irb(main):001:0> Auction.running_auctions
=> #<Mongoid::Criteria
selector: {:end_time=>{"$gt"=>Fri Jul 22 00:42:38 UTC 2011}},
options: {:sort=>[:end_time, "ASC"]},
class: Auction,
embedded: false>
Notice that my date here is Fri Jul 22 00:42:38 UTC 2011
irb(main):002:0> DateTime.now
=> Fri, 22 Jul 2011 11:42:56 +0530
irb(main):003:0> DateTime.now.utc
=> Fri, 22 Jul 2011 06:12:59 +0000
Notice here that my datetime is Fri, 22 Jul 2011 06:12:59 +0000
What is making query date older than actual current date ? Does mogoid or mongodb doing caching there ?
Please let me know if I am missing something.
UPDATE
Loading development environment (Rails 3.0.7)
irb(main):001:0> Auction.running_auctions(DateTime.now.utc)
=> #<Mongoid::Criteria
selector: {:end_time=>{"$gt"=>Fri Jul 22 01:21:53 UTC 2011}},
options: {:sort=>[:end_time, "ASC"]},
class: Auction,
embedded: false>
irb(main):002:0> DateTime.now.utc
=> Fri, 22 Jul 2011 06:52:03 +0000
You have to use a lambda there. As of now, the DateTime.now.utc is computed at application startup and cached with application code.
You need to write your scope as:
scope :running_auctions, lambda {
{
:where => { :end_time.gt => DateTime.now.utc },
:order_by => [:end_time, "ASC"]
}
}

Getting Private Method error in Ruby on Rails

I have a controller:
class StatsController < ApplicationController
require 'time'
def index
#started = "Thu Feb 04 16:12:09 UTC 2010"
#finished = "Thu Feb 04 16:13:44 UTC 2010"
#duration_time = stats_duration(#started, #finished)
end
private
def stats_duration(started, finished)
time_taken = distance_of_time_in_words(Time.parse(started), Time.parse(finished))
time_taken
end
end
It takes in a start and end time and calculates the duration between the times.
When I run this I get the following error:
private method `gsub!' called for Thu
Feb 04 16:12:09 UTC 2010:Time
Why is this happening?
private method gsub! called when using Time.parse usually means that you have called parse with a Time object rather than a String so it sounds like your code is actually trying to parse the time twice.
e.g.
>> t = Time.now
=> Fri Feb 05 13:12:17 +0000 2010
>> Time.parse(t)
NoMethodError: private method `gsub!' called for Fri Feb 05 13:12:17 +0000 2010:Time
from c:/ruby/lib/ruby/1.8/date/format.rb:965:in `_parse'
from c:/ruby/lib/ruby/1.8/time.rb:240:in `parse'
from (irb):6

Resources