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

术语.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 验证似乎不起作用,即它接受 term_type 的任何值。当使用包含(或产生相同失败条件的 validates_inclusion_of)时,我是否需要与模型中的那些匹配的数据库验证?我应该警惕的字符串有什么问题吗?

Term_type 只是数据库中的一个字符串,而不是一个单独的表。

任何指针都非常感谢。

文斯

4

1 回答 1

6

您是否尝试过使用

validates :field, :inclusion => %{abc ade aeee}
于 2011-02-18T11:57:28.297 回答