目前,当尝试通过 Rails 控制台或种子数据将新用户添加到我的用户表时,一切都为零。
在我的种子文件中,我正在运行:
u1 = User.create(:from_email => "[email protected] /cdn-cgi/l/email-protection", :to_email => "[email protected] /cdn-cgi/l/email-protection", :from_name=>"Vero", :to_name=>"Spencer", :message=>"Test message", :password=>"chicken", :id => 1)
u1.save!
如果我通过 Rails 中的种子数据或通过 Rails 控制台运行此命令,然后使用 User.all 检查 Postgres 数据库,我会得到:
#<User id: 1, from_name: nil, to_name: nil, from_email: nil, to_email: nil, message: nil, created_at: "2014-12-10 02:34:02", updated_at: "2014-12-10 02:34:02", password: nil>
我已经处理这个问题几个小时了,开始变得很困惑。我将不胜感激,我的模型和架构如下:
class User < ActiveRecord::Base
attr_accessor :from_name, :from_email, :to_name, :to_email, :message, :password, :id
before_save { self.from_email = from_email.downcase }
before_save { self.to_email = to_email.downcase }
before_save { self.password = "merrychristmas#{ Random.rand(1000) }" }
validates :id, :presence => true, :uniqueness => true
validates :from_name, :presence => true, :uniqueness => false
validates :to_name, presence: true, :uniqueness => false
validates :from_email, presence: true, format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i}
validates :to_email, presence: true, format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i }
validates :message, presence: true, length: { maximum: 50 }
def self.authenticate(id, password)
user = find(id)
if user && user.password
user
else
nil
end
end
end
Schema:
ActiveRecord::Schema.define(version: 20141209104326) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "users", force: true do |t|
t.string "from_name"
t.string "to_name"
t.string "from_email"
t.string "to_email"
t.text "message"
t.datetime "created_at"
t.datetime "updated_at"
t.string "password"
end
end
SQL
(0.3ms) BEGIN
User Exists (0.5ms) SELECT 1 AS one FROM "users" WHERE "users"."id" = 2 LIMIT 1
SQL (0.4ms) INSERT INTO "users" ("created_at", "updated_at") VALUES ($1, $2) RETURNING "id" [["created_at", "2014-12-10 04:15:33.579478"], ["updated_at", "2014-12-10 04:15:33.579478"]]
(6.5ms) COMMIT
=> #<User id: nil, from_name: nil, to_name: nil, from_email: nil, to_email: nil, message: nil, created_at: "2014-12-10 04:15:33", updated_at: "2014-12-10 04:15:33", password: nil>