Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 正在验证其他对象属性上的ActiveRecord属性长度解除挂起_Ruby On Rails_Ruby_Ruby On Rails 3_Activerecord - Fatal编程技术网

Ruby on rails 正在验证其他对象属性上的ActiveRecord属性长度解除挂起

Ruby on rails 正在验证其他对象属性上的ActiveRecord属性长度解除挂起,ruby-on-rails,ruby,ruby-on-rails-3,activerecord,Ruby On Rails,Ruby,Ruby On Rails 3,Activerecord,我正在尝试创建一个验证,如果当前对象的country\u code设置为ES,则将:phone字段的长度强制为9位 我试着做这样的事情,但没有成功 validates_length_of :phone, :is => 9, :message => "number needs to be at least 9 digits." if self.country_code == "ES" 我想你要找的是: validates_length_of :phone, :is => 9,

我正在尝试创建一个验证,如果当前对象的
country\u code
设置为
ES
,则将
:phone
字段的长度强制为9位

我试着做这样的事情,但没有成功

validates_length_of :phone, :is => 9, :message => "number needs to be at least 9 digits." if self.country_code == "ES"

我想你要找的是:

validates_length_of :phone, :is => 9,
:message => "number needs to be at least 9 digits.",
:if => "country_code == 'ES'"

看看这些文件

validates_length_of :phone, :is => 9,
:message => "number needs to be at least 9 digits.",
:if => Proc.new { |a| a.country_code == 'ES' }