named_scope conditions and timezone mismatch - ruby-on-rails

Here is the setup:
end_date = DateTime.parse('2010-01-01 12:00:00-04')
and sometimes it's initialized:
end_date = 1.days.ago
The question... do these named_scope(s) generate the same EXACT SQL?
named_scope :before, lambda { |end_date|
{ :conditions =>
["overdraft_transaction_payments.created_at < ?", end_date] }
}
and
named_scope :before, lambda { |end_date|
{ :conditions =>
["overdraft_transaction_payments.created_at < ?", end_date.utc] }
}
In the first example I use end_date and in the second I use end_date.utc.
(It may be important to note.... The DB server's OS is set to CDT, and the DB uses UTC internally. The rails server's OS is set to CDT and the application instance is set to EDT. I realize that this is probably not the optimum way to configure these systems, however, the issue at hand is the ActiveRecord output.)
For what it is worth my intuition says that the first example is going to generate a time string for the local TZ where the second is going to generate a UTC-0.
PS: Is there a mini test case I can use to validate my intuition?

I believe under the hood the date is going to be converted with a call to ".to_s(:db)" and you can see in an IRB console session what that's going to return:
>> DateTime.parse('2010-01-01 12:00:00-04').to_s(:db)
=> "2010-01-01 12:00:00"
>> DateTime.parse('2010-01-01 12:00:00-04').utc.to_s(:db)
=> "2010-01-01 16:00:00"
>> 1.days.ago.to_s(:db)
=> "2010-08-12 18:01:09"
>> 1.days.ago.utc.to_s(:db)
=> "2010-08-12 18:01:13"

Related

A lambda expression in a rails validation statement

I am using the timeliness gem to do time and date validation.
My event model has a time field storing a string like "11:15", a start_date and end_date storing a Date object.
I want the following validation behaviour:
If a user tries to create an event on the current day of the year (so start_date == Date.today), and the time is in the past (so if it was 15:30 when the event was created and the user entered 11:30), then the validation should fail. I.e we only want events created where the date is today or is in the future, and the time, if the date is today, is in the future.
I am trying the following validation for this:
validates :time, :presence => true,
:timeliness => { :type => :time, :on_or_after => lambda { if(:start_date == Date.today && :day_of_week.downcase == Date.today.strftime("%A").downcase) then return Time.new(:time.to_s).in_time_zone(:timezone)>Time.now.in_time_zone(:timezone) else return true end } }
However it is not behaving correctly. I.e I am able to create an event with the start_date of 03/08/2015 and a time of 09:00 despite it being 15:31 on 03/08/2015! Please help!
Your lambda doesn't make a lot of sense, because you're using symbols everywhere - :start_date == Date.today will always be false. The value of lambdas in validations is that they're passed your Event instance, so you can reference event.start_date. I agree with #arthur.karganyan in the comments that if you need something this complicated, it'll be easier to work with as a method.
But, you're also making this much more complicated than you need to. It looks like you have start_date and time as separate attributes, which is difficult to work with because they'll tend to rely on each other. I'd recommend making those a single start_time attribute instead, and then you can use validates_timeliness like so:
validates_datetime :start_time, on_or_after: -> { Time.now }
If you must have separate attributes, your validations might look like:
validates_date :start_date, on_or_after: -> { Date.today }
validates_time :time, on_or_after: lambda { |event| Time.now if event.start_date.today? }
This validates that the date is today or in the future, and if it's today, also checks the time. validates_timeliness appears to accept any time when on_or_after lambda evaluates to nil.

How to get all documents and hide this which are expired if they are defined?

I want to hide past events if they are defined and get all other. How to show all documents even if :once_at is nil and if :once_at is defined then hide these ones which are expired?
My recent approach, shows only events with defined :once_at, (I tryed with :once_at => nil, but without results):
default_scope where(:once_at.gte => Date.today)
or (also not working)
default_scope excludes(:once_at.lte => Date.today)
When do you think Date.today is evaluated? If you say this:
default_scope where(:once_at.gte => Date.today)
Date.today will be evaluated when the class is being loaded. This is almost never what you want to happen, you usually want Date.today to be evaluated when the default scope is used and the usual way to make that happen is to use a proc or lambda for the scope:
default_scope -> { where(:once_at.gte => Date.today) }
The next problem is what to do about documents that don't have a :once_at or those with an explicit nil in :once_at. nil won't be greater than today so you'd best check your conditions separately with an :$or query:
default_scope -> do
where(
:$or => [
{ :once_at => nil },
{ :once_at.gte => Date.today }
]
)
end

Calculating non-Rolling Last Week in Rails

I have a model called Article and what I want to do is show all of the Articles where the created_at date was for last week. I understand how to a rolling week (ie the last 7 days to now), such as this question.
scope :last_week, lambda { :conditions => { :created_at => 1.week.ago..DateTime.now.end_of_day } }
However, what I want to do is to find the Articles that were Sunday - Saturday of last week, regardless of what day the current day is.
Similar to John's answer, but I think this fits your situation a little better:
scope :last_week, lambda { { conditions: { created_at: last_week_range } } }
def self.last_week_range
1.week.ago.beginning_of_week(:sunday)..1.week.ago.end_of_week(:sunday)
end
Date.parse gives an awesome results being run on the weekday name:
require 'date'
# ⇒ true
Date.today
# ⇒ <Date: 2013-04-02 ((2456385j,0s,0n),+0s,2299161j)>
Date.parse('Sunday')
# ⇒ <Date: 2013-03-31 ((2456383j,0s,0n),+0s,2299161j)>
Date.parse('Wednesday')
# ⇒ <Date: 2013-04-03 ((2456386j,0s,0n),+0s,2299161j)>
So all you need is:
def last_weekend
d = Date.parse('Saturday')
d -= 7 if d > Date.today
Range.new d, d+1
end
Hope this helps.
Rails provides a helper method to get the beginning and end of a week for a given date. The method defaults to monday so you have to pass in sunday to override it.
scope :last_week, lambda { :conditions => { :created_at => Date.today.at_beginning_of_week(:sunday)..Date.today.at_end_of_week(:sunday) } }
I have usen Date.today as an example but you could have the scope have a date argument so you could apply any date to the scope.
Edit:
scope :last_week, lambda{ |date| {:conditions => ((date - 1.week).at_beginning_of_week(:sunday))..((date-1.week).at_end_of_week(:sunday)) }}
then just call the scope as
last_week(some_date)

Proc.new in Ruby: when do I need to use it?

The date_validator in its examples has a comment:
Using Proc.new prevents production cache issues
Does it mean, that everywhere in my code, where I use current time related methods (Time.now, 1.day.since(Time.zone.now), etc.) I should surround them with Proc.new { }?
I don't completely understand this, since replacing
time_now = Time.now.utc
with
time_now = Proc.new { Time.now.utc }
just doesn't make sense to me (new type of object is returned).
So, the question is, when and how should I use Proc.new with time related methods? And does that still apply to the latest versions of Ruby (1.92) and Rails (3.1)?
No, it only references the given example:
validates :expiration_date,
:date => {:after => Proc.new { Time.now },
:before => Proc.new { Time.now + 1.year } }
If instead you'd written
validates :expiration_date,
:date => {:after => Time.now,
:before => Time.now + 1.year }
Time.now would be interpreted when the class is parsed and it would be validating against that value.
Using Proc.new in that validation means Time.new will be evaluated when the validation is actually run - not when it's initially being interpreted.
What Proc.new (and lambda) does is, save all your statements in their original form (in an anonymous function), and doesn't evaluate them.
Date Validator gem must have some kind of test to check if a Proc was passed, and it evaluates it when it's actually validating the stuff.
Edit: It does this here - https://github.com/codegram/date_validator/blob/master/lib/active_model/validations/date_validator.rb#L47
option_value = option_value.call(record) if option_value.is_a?(Proc)
A quick example :
pry(main)> time_now = Time.now
=> 2011-06-19 21:07:07 +0530
pry(main)> time_proc = Proc.new { Time.now }
=> #<Proc:0x9710cc4#(pry):1>
pry(main)> time_proc.call
=> 2011-06-19 21:07:28 +0530
pry(main)> time_proc.call
=> 2011-06-19 21:07:31 +0530
pry(main)>
Note that this will only work with libraries that do implement this kind of check, and not every function accepting a Time.

named_scope for date.today Rails 2.3.9

Working with a Rails 2.3.9 app and wondering how to write my named_scope such that I only get workouts from the current date. I am setting the timezone in in the application controller with a before_filter. The below doesn't throw an error, just doesn't filter:
workout.rb
named_scope :today_only, :conditions => [ "workouts.created_at <= ? AND workouts.created_at >= ?", Time.zone.now, 1.days.ago ]
application_controller.rb
before_filter :set_user_time_zone
You're not seeing the responses you want because Ruby is evaluating your call to Time.now when it evaluates your class definition, not when you're calling the scope. You need to pass a lambda to your named_scope call to get it to evaluate on every request:
# ensure that Time.now is evaluated on every call
named_scope :today_only, lambda {{ :conditions => ["workouts.created_at BETWEEN ? AND ?", Time.zone.now.at_beginning_of_day, Time.zone.now.end_of_day ] }}
Also, I think your Time boundaries may be incorrect. Are you looking for workouts that were created in the past 24 hours (relative to Time.now), or only workouts that were created "today?" Your example works for the former, the example above does the latter.

Resources