更新:我的意思是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工作?
答案 0 :(得分:1)
您需要使用验证...
# model.rb
validates :field, :presence => true, :length => {:minimum => 1, :maximum => 9}
另一方面,为什么你使用seeds.rb?这是迁移