Validates_inclusion_of no longer works in Rails 4.1?

In the following code, make sure time_zone selected in time zones in ActiveSupport::TimeZone.us_zones :

 validates_inclusion_of :time_zone, in: ActiveSupport::TimeZone.zones_map(&:name) 

Works great in Rails 4.0. Just upgraded to Rails 4.1, and I get this error on my index page (just by simply looking at the models):

An object with the #include method? or proc, lambda or character is required and should be provided as: in (or: inside) hash configuration option

I assume ActiveSupport::TimeZone.zones_map(&:name) no longer a valid value for the in property?

+7
ruby ruby-on-rails ruby-on-rails-4 activesupport
source share
3 answers

try adding .keys ?

 validates :time_zone, inclusion: { in: ActiveSupport::TimeZone.zones_map.keys } 
+22
source share

In Rails 5 , ActiveSupport::TimeZone.zones_map is a private method. Therefore, if you want your validation to work, I suggest the following syntax:

 validates :time_zone, inclusion: { in: ActiveSupport::TimeZone.all.map(&:name) } 
+2
source share

If you want to continue using validates_inclusion_of , this also works:

 validates_inclusion_of :time_zone, :in => ActiveSupport::TimeZone.zones_map(&:name).keys, :message => "is not a valid time zone" 
+1
source share

All Articles