2016-10-21 4 views
0

私はMichael HartlのLearn Railsチュートリアルの改訂版で作業しています。私は第6章のモデリングユーザーです。何らかの理由で、ユーザーがActiveRecordで正しく作成されておらず、まったく保存していません。Rails 5:ActiveRecordがレコードを作成していません

私はその後、私はrails db:seedを実行するが、私は私のrails consoleに行けば何のユーザーが作成されていないように、それが表示された私のseeds.rbファイル

user_1 = User.create(id: 1, name: 'Han Solo', email: '[email protected]') 
user_2 = User.create(id: 2, name: 'Luke Skywalker', email: '[email protected]') 

でこれらのユーザーを配置:

Running via Spring preloader in process 24358 
Loading development environment (Rails 5.0.0.1) 
2.2.2 :001 > User.delete_all 
    SQL (1.1ms) DELETE FROM "users" 
=> 0 
2.2.2 :002 > 

user.rbユーザーモデル

class User < ApplicationRecord 
    #Ensure Email Uniqueness by Downcasing the Email Attribute 
    before_save {self.email = email.downcase } 
    #validates name, presence, and length 
    validates :name, presence: true, length: { maximum: 100 } 
    #Validate presence, length, format, and uniqueness (ignoring case) 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, length: {maximum: 250}, format: {with: VALID_EMAIL_REGEX }, uniqueness: {case_sensitive: false} 
    #Adds ability to save securely hashed password_digest attribute to database 
    #Adds a pair of virtual attributes (password and password_confirmation) 
    #including presence validations upon object creation and a validation 
    #requiring that they match 
    #adds authenticate method that returns the user when the password is correct (and false otherwise) 
    has_secure_password 
    PASSWORD_FORMAT = /\A 
     (?=.{8,})   # Must contain 8 or more characters 
     (?=.*\d)   # Must contain a digit 
     (?=.*[a-z])  # Must contain a lower case character 
     (?=.*[A-Z])  # Must contain an upper case character 
     (?=.*[[:^alnum:]]) # Must contain a symbol 
    /x 
    validates :password, presence: true, length: {minimum: 8}, format: {with: PASSWORD_FORMAT} 

end 

schema.rb 

ActiveRecord::Schema.define(version: 20161020211218) do 

    # These are extensions that must be enabled in order to support this database 
    enable_extension "plpgsql" 

    create_table "users", force: :cascade do |t| 
    t.string "name" 
    t.string "email" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    t.string "password_digest" 
    t.index ["email"], name: "index_users_on_email", unique: true, using: :btree 
    end 

end 

誰も何が起こっているかもしれない考えている?

+0

あなたのデータベースには既にそのユーザーがいますか? 'valid? 'をチェックすると表示されます。電子メールアドレスがすでに使用されているためfalseを返しています。 – rdubya

+1

実際のgmailアドレスではなく、IANAサンドボックスドメインの '@ example.com'を例に挙げてください。アドレスを所有している誰でも、スパムロボットによって収穫されるのを防ぐことができます。 'user.valid?'を呼び出した後に – max

+0

を呼び出すと、 'user.errors.full_messages'を呼び出して、何が同じものを防ぐのかを確認します。 –

答えて

1

わかりました。

  1. 私はseed.rbからIDを取り出しました。私はid属性を取り出したとき、有効なパスワードを持っていないユーザーのエラーを投げ始めました。その後、私のユーザモデルに設定されているパスワード標準に準拠するために、パスワードとパスワードの確認を追加しました。
  2. マイモデルが更新され、パスワード確認が追加されました。

は、ここでは私の私は、ユーザーが、レールコンソールを開くUser.delete_allを実行し、そしてそれらを拭き取っ見て正しく作成されていることをテストしuser.rb

class User < ApplicationRecord 
    #Ensure Email Uniqueness by Downcasing the Email Attribute 
    before_save {self.email = email.downcase } 
    #validates name, presence, and length 
    validates :name, presence: true, length: { maximum: 100 } 
    #Validate presence, length, format, and uniqueness (ignoring case) 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, length: {maximum: 250}, format: {with: VALID_EMAIL_REGEX }, uniqueness: {case_sensitive: false} 
    #Adds ability to save securely hashed password_digest attribute to database 
    #Adds a pair of virtual attributes (password and password_confirmation) 
    #including presence validations upon object creation and a validation 
    #requiring that they match 
    #adds authenticate method that returns the user when the password is correct (and false otherwise) 
    has_secure_password 
    PASSWORD_FORMAT = /\A 
     (?=.{8,})   # Must contain 8 or more characters 
     (?=.*\d)   # Must contain a digit 
     (?=.*[a-z])  # Must contain a lower case character 
     (?=.*[A-Z])  # Must contain an upper case character 
     (?=.*[[:^alnum:]]) # Must contain a symbol 
    /x 
    validates :password, presence: true, length: {minimum: 8}, format: {with: PASSWORD_FORMAT} 
    validates :password_confirmation, presence: true 
end 

そしてseeds.rb

user_1 = User.create!(name: 'Han Solo', email: '[email protected]', password: 'Password123!', password_confirmation: 'Password123!') 
user_2 = User.create!(name: 'Luke Skywalker', email: '[email protected]', password: 'Password123!', password_confirmation: 'Password123!') 

を更新していますSQLレコード

+0

レコードにIDを割り当てない - データベースはID列を自動的にインクリメントし、INSERT SQLクエリの一部としてIDを返します。 – max

+0

再確認していただき、ありがとうございます。ちゃんと覚えておきますよ。 –

関連する問題