Search code examples
ruby-on-railsvalidationinclusion

Rails inclusion validation


I'm struggling with the following inclusion validation,

class User < ActiveRecord::Base
  attr_accessible :language

  validates :language, :presence => true, :inclusion => { :in => I18n.available_locales.join(' ')}
end

If I run this in the console,

u = User.new
u.valid?

then I get a TypeError: can't convert nil into String in the include? method of active_model/validations/inclusion.rb.

However, if I change the validation line to,

validates :language, :presence => true, :inclusion => { :in => %(en fr es)}

then the validation works as expected. It is invalid if language is either nil or not one of the entries in the list as you would expect and there is no crashing.

I've stepped through the code to verify that it is generating the inclusion list okay, which it is. So why does it crash? Shouldn't the presence validation pick up the problem and prevent any further validation? And why does it crash when I generate the list as opposed to hardcoding the values?

I even tried using the proc form of :in to see if that made any difference, which it didn't. But then I didn't really expect that to be needed because I only want to generate the list once when the app loads anyway since I18n.available_locales will never change during the execution of the app.

UPDATE: I had an idea and tested the following code,

class User < ActiveRecord::Base
  attr_accessible :language

  validates :language, :presence => true, :inclusion => { :in => ['en','fr','es'].join(' ') }
end

This code also generates the same error, so the problem is not with I18n or anything like that. It has to do with a difference between %(en fr es) and ['en','fr','es'].join(' ').


Solution

  • The reason your second condition works is that #include? is defined for the String class:

    > "qwertyuiop".include? "tyu"
    true
    

    so if you validate inclusion in %(en fr es) (which is exactly "en fr es") a value "n f" would pass the validation.

    You have to use the array (either as literal [] or as words %w()). If you want to use I18n.available_locales be sure to convert them to strings

    validates :language, :inclusion => { :in => I18n.available_locales.map(&:to_s) }
    

    I can't reproduce your error, but the :presence validation is useless, as the :inclusion already checks for the value to be in the list.