在seeds.rb中,我的消息模型只有一条记录:
Message.create!(email: "example@example.com",
name: "Example User",
content: "This is my message")
如果我运行rake db:seed
,我会收到错误消息:
rake aborted!
ActiveRecord::RecordInvalid: Validation failed: Email has already been taken, Username has already been taken
/usr/local/rvm/gems/ruby-2.1.5/gems/activerecord-4.2.1/lib/active_record/validations.rb:79:in `raise_record_invalid'
...
如果我运行bundle exec rake db:reset
,我会得到错误:
-- initialize_schema_migrations_table()
-> 0.0734s
rake aborted!
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: NOT NULL constraint failed: messages.name: INSERT INTO "messages" ("created_at", "updated_at") VALUES (?, ?)
/usr/local/rvm/gems/ruby-2.1.5/gems/sqlite3-1.3.10/lib/sqlite3/statement.rb:108:in `step'
...
邮件模型中的电子邮件确实是必需的。但当我重置数据库时,我仍然不明白这个记录怎么会无效。电子邮件不需要是唯一的,那么为什么seed命令会给出这样的错误呢?
消息的模型文件:
attr_accessor :name, :email, :content
validates :name, presence: true,
length: { maximum: 255 }
VALID_EMAIL_REGEX = /A[w+-.]+@[a-zd-]+(.[a-zd-]+)*.[a-z]+z/i
validates :email, presence: true,
length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX }
validates :content, presence: true,
length: { maximum: 600 }
消息迁移文件:
def change
create_table :messages do |t|
t.string :name, null: false, limit: 255
t.string :email, null: false, limit: 255
t.string :content, null: false, limit: 600
t.timestamps null: false
end
end
我不确定是什么导致了这个问题。。。
更新:我迷路了,即使没有message.create,我现在在播种时也会收到一条错误消息!种子文件中的行。错误显示rake aborted! ActiveRecord::RecordInvalid: Validation failed: Email has already been taken, Username has already been taken
。但是,自从我第一次运行bundle exec rake db:reset
以来,这怎么可能呢?这不是从数据库中删除了所有数据吗?从定义上讲,这不是意味着它们不能被获取吗?
我的种子文件是:
User.create!(fullname: "Example User",
username: "fakename0",
email: "example@railstutorial.org",
admin: true,
activated: true,
activated_at: Time.zone.now,
password: "foobar",
password_confirmation: "foobar")
User.create!(fullname: "Example User 2",
username: "fawwkename0",
email: "exaaample@railstutorial.org",
admin: false,
activated: true,
activated_at: Time.zone.now,
organization: "organization",
password: "foobar",
password_confirmation: "foobar")
99.times do |n|
username = "fakename#{n+1}"
email = "example-#{n+1}@railstutorial.org"
password = "password"
User.create!(username: username,
email: email,
password: password,
password_confirmation: password,
activated: true,
activated_at: Time.zone.now)
更新2:我发现运行rake db:reset
已经为数据库播种(或者至少在该命令之后数据库不是空的)。尽管如此,这并不能解释为什么我不能用播种
Message.create!(email: "example@example.com",
name: "Example User",
content: "This is my message")
对该数据进行种子设定会产生错误:rake aborted!
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: NOT NULL constraint failed: messages.name: INSERT INTO "messages" ("created_at", "updated_at") VALUES (?, ?) /usr/local/rvm/gems/ruby-2.1.5/gems/sqlite3-1.3.10/lib/sqlite3/statement.rb:108:in 'step' ...
从模型文件中删除attr_accessor :name, :email, :content
解决了这个问题。不知道为什么。我添加这一行是因为它包含在教程中:http://matharvard.ca/posts/2014/jan/11/contact-form-in-rails-4/但如果没有这一行,信息表单似乎仍然有效。