是否可以在Rails中的seed.rb文件中限制字符串列的长度?

时间:2011-01-23 02:43:05

标签: ruby-on-rails ruby-on-rails-3

更新:我的意思是db / migrate中的creates_coves.rb文件,而不是seeds.rb

在我的seed.rb文件中,我有:

class CreateCoves < ActiveRecord::Migration
  def self.up
    create_table :coves do |t|
      t.string :title, :limit=>9,:null =>false

      t.timestamps
    end
  end

  def self.down
    drop_table :coves
  end
end

运行rake db:migrate后,我仍然可以创建标题超过9个字符的新对象,也可以创建空字符。为什么不是限制或null工作?

1 个答案:

答案 0 :(得分:1)

您需要使用验证...

Rails 3 Validations

# model.rb
validates :field, :presence => true, :length => {:minimum => 1, :maximum => 9}

另一方面,为什么你使用seeds.rb?这是迁移