RAILS 3 model validation inclusion in does not work

若如初见. 提交于 2019-12-13 14:43:53

问题


# ==Schema Information
# Schema version:
# Table name: terms
#  id                 :integer         not null, primary key
...
#  user_id            :integer
#  term_type          :string(255)`

term.rb:

  class Term < ActiveRecord::Base 
validates   :term_type,         :inclusion => { :in => %w(Term Segment Entity Abbreviation) }`

Rails 3.0.3, ruby 1.8.7, Windows via Cygwin The validation does not seem to work, i.e. it accepts any value for term_type. When using inclusion (or validates_inclusion_of which produces the same fail condition) do I need DB validations matching those in the model? Any issues with Strings I should be wary of?

Term_type is just a string in the DB and not a separate table.

Any pointers much appreciated.

Vince


回答1:


Have you tried using

validates :field, :inclusion => %{abc ade aeee}


来源:https://stackoverflow.com/questions/5040661/rails-3-model-validation-inclusion-in-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!