Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(' ').

like image 233
richard Avatar asked Sep 30 '12 08:09

richard


People also ask

How does validation work in Rails?

Rails validation defines valid states for each of your Active Record model classes. They are used to ensure that only valid details are entered into your database. Rails make it easy to add validations to your model classes and allows you to create your own validation methods as well.

What is the difference between validate and validates in rails?

validates is used for normal validations presence , length , and the like. validate is used for custom validation methods validate_name_starts_with_a , or whatever crazy method you come up with. These methods are clearly useful and help keep data clean. That test fails.

What is Active Record in Ruby on Rails?

What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.


2 Answers

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.

like image 192
rewritten Avatar answered Sep 28 '22 05:09

rewritten


You should pass an array into the inclusion validation, not a string. So in your example with %(en fr es) if you use %w(en fr es) it works fine.

like image 26
Vasiliy Ermolovich Avatar answered Sep 28 '22 04:09

Vasiliy Ermolovich