RAILS 3 模型验证包含不起作用

发布于 10-18 04:28 字数 633 浏览 5 评论 0原文

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

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

非常感谢任何指点。

文斯

# ==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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吹泡泡o2024-10-25 04:28:22

你有没有尝试过使用

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

Have you tried using

validates :field, :inclusion => %{abc ade aeee}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文