How can I get current year in Ruby on Rails?
I tried a variety of things, including
The problem is they return the previous year in cases where year changes after launching the server (eg. after new year's).
Relevant code:
Model brewery.rb
class Brewery < ActiveRecord: :Base
...
validates :year, numericality: { only_integer: true, less_than_or_equal_to: Date.today.year }
...
Problem occurs when creating a new brewery, so I assumed Date.today.year would be evaluated whenever that action takes place.
In your example Date.today.year
is evaluated only once when the class is loaded and therefore doesn't change later on.
When you use a lambda in your validator declaration then it re-evaluates the block each time when it runs the validation for that attribute:
validates :year, numericality: {
only_integer: true,
less_than_or_equal_to: ->(_brewery) { Date.current.year }
}
Furthermore, I suggest using Date.current
instead of Date.today
because the current
method pays attention to timezone settings.
<%= Time.zone.now.year %>
-- factors in time zone
Actually, it's better to use Time.current
instead of Time.now
in Rails, because the first one adopts current time zone.
http://edgeguides.rubyonrails.org/active_support_core_extensions.html#time-current
@spickermann's recipe seems to be correct, I'll just try to explain why it works.
The code you write inside your class is executed at compile time, when Ruby compiles to the VM bytecode. Therefore, the Date.today.year
in your validator is calculated exactly once, when the class is compiled.
By introducing the lambda you enable ActiveRecord to use the return value of said lambda. The lambda itself will be executed with each validation.
Hope that made the situation clearer!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With