Rails Migration 问题

279959599 · March 15, 2014 · Last by 279959599 replied at March 01, 2015 · 1910 hits

在书写 migration 中,某个字段限制在几个选项中,该怎么写呢?比如 status 字段,只能是 yes,no,apply 三个之一

非要寫在 migration 裏面?數據庫有這個類型才行吧。我也感覺這個是 model 的邏輯。

class Tweet < ActiveRecord::Base
  validates :status, inclusion: %w(yes no apply)
end

这个限制写在模型里最好,这个无争议呀亲。

大家说的都对呀,我是有点强迫症那种,什么东西都要做完美,数据库确实不能限制的太死了,在 model 中控制最好

You need to Sign in before reply, if you don't have an account, please Sign up first.