model
class User
attr_accessable :xrk_user_id, :user_name, :status, :user_type,:encrypted_password, :password, :current_password
end
db/migrate :reate_table
def change
create_table :users, comment: "用户表" do |t|
t.string :user_name, null: false, comment: "用户姓名"
t.integer :user_type, null: false, comment: "用户类型 0:审核人 1:提单人"
t.boolean :status, null: false, comment: "用户状态 false: 无 效 true: 有 效"
t.integer :xrk_user_id, null: false, comment: "用户ID"
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
t.string :encrypted_password, :null => false, :default => ""
## Rememberable
t.datetime :remember_created_at
## Trackable
t.integer :sign_in_count, :default => 0, :null => false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip
## Lockable
t.integer :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts
t.string :unlock_token # Only if unlock strategy is :email or :both
t.datetime :locked_at
t.timestamps
end
console(rails c)
>> user_attr = {:xrk_user_id=>8, :user_name=>"dev", :user_type=>1, :password=>"111111", :status=>true}
=> {:xrk_user_id=>8, :user_name=>"dev", :user_type=>1, :password=>"111111", :status=>true}
>> user = User.new user_attr
=> #<User id: nil, user_name: "dev", user_type: 1, status: true, xrk_user_id: 8, reset_password_token: nil, reset_password_sent_at: nil, encrypted_password: "$2a$10$c8fJb8GKGMg.YmwWcCT86uuImVwQ.JTtZ/8RXIM90T4k...", remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, failed_attempts: 0, unlock_token: nil, locked_at: nil, created_at: nil, updated_at: nil>
>> user.save
INSERT INTO "users" ("created_at", "encrypted_password", "status", "updated_at", "user_name", "user_type") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["created_at", Mon, 31 Mar 2014 09:38:59 CST +08:00], ["encrypted_password", "$2a$10$c8fJb8GKGMg.YmwWcCT86uuImVwQ.JTtZ/8RXIM90T4kz7XB01SEa"], ["status", true], ["updated_at", Mon, 31 Mar 2014 09:38:59 CST +08:00], ["user_name", "dev"], ["user_type", 1]]
PG::NotNullViolation: ERROR: null value in column "xrk_user_id" violates not-null constraint
: INSERT INTO "users" ("created_at", "encrypted_password", "status", "updated_at", "user_name", "user_type") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"
(0.2ms) ROLLBACK
ActiveRecord::StatementInvalid: PG::NotNullViolation: ERROR: null value in column "xrk_user_id" violates not-null constraint
: INSERT INTO "users" ("created_at", "encrypted_password", "status", "updated_at", "user_name", "user_type") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"
在 new 一个 user 的时候 xrk_user_id 时有值,但是 save 的时候却被清空了。请问这个是什么问题啊????,在线等啊....(数据库用的是 PG)