2013-04-17 25 views
12

テストを実行中にこのエラーが発生しています。私はemail_confirmationのすべてが正しく綴られていることを確認しています(私が狂っていない限り)。私はRails noobのビットなので、単純なものかもしれません。"ActiveRecord :: UnknownAttributeError:unknown attribute:email_confirmation" rspecのエラー

ユーザーモデル

class User < ActiveRecord::Base 
    attr_accessible :email, :email_confirmation, :first_name, :last_name, 
        :password, :password_confirmation 
    has_secure_password 

    before_save { |user| user.email = email.downcase } 

    validates :first_name, presence: true, length: { maximum: 25 } 
    validates :last_name, presence: true, length: { maximum: 25 } 
    VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX }, 
        uniqueness: { case_sensitive: false } 
    validates :email_confirmation, presence: true 
    validates :password, presence: true, length: { maximum: 6 } 
    validates :password_confirmation, presence: true 
end 

RSpecのは、あなたが呼ばれるあなたのusers表の列を持っていないので、あなたがUnknownAttributeErrorを取得している

require 'spec_helper' 

describe User do 
    before { @user = User.new(email: "[email protected]", 
          first_name: "John", last_name: "Smith", 
          password: "foobar", password_confirmation: "foobar", 
          email_confirmation: "[email protected]") } 

    subject { @user } 

    it { should respond_to(:first_name) } 
    it { should respond_to(:last_name) } 
    it { should respond_to(:email) } 
    it { should respond_to(:email_confirmation) } 
    it { should respond_to(:password_digest) } 
    it { should respond_to(:password) } 
    it { should respond_to(:password_confirmation) } 
    it { should respond_to(:authenticate) } 

    it { should be_valid } 

    describe "when first name is not present" do 
    before { @user.first_name = " " } 
    it { should_not be_valid } 
    end 

    describe "when last name is not present" do 
    before { @user.last_name = " " } 
    it { should_not be_valid } 
    end 

    describe "when email is not present" do 
    before { @user.email = @user.email_confirmation = " " } 
    it { should_not be_valid } 
    end 

    describe "when password is not present" do 
    before { @user.password = @user.password_confirmation = " " } 
    it { should_not be_valid } 
    end 

    describe "when first_name is too long" do 
    before { @user.first_name = "a" * 26 } 
    it { should_not be_valid } 
    end 

    describe "when last_name is too long" do 
    before { @user.last_name = "a" * 26 } 
    it { should_not be_valid } 
    end 

    describe "when email format is invalid" do 
    it "should be invalid" do 
     addresses = %w[[email protected],com user_at_foo.org [email protected] 
          [email protected]_baz.com [email protected]+baz.com] 
     addresses.each do |invalid_address| 
     @user.email = invalid_address 
     @user.should_not be_valid 
    end  
    end 
    end 

    describe "when email format is valid" do 
    it "should be valid" do 
     addresses = %w[[email protected] [email protected] [email protected] [email protected]] 
     addresses.each do |valid_address| 
     @user.email = valid_address 
     @user.should be_valid 
     end  
    end 
    end 

    describe "when email address is already taken" do 
    before do 
     user_with_same_email = @user.dup 
     user_with_same_email.email = @user.email.upcase 
     user_with_same_email.save 
    end 

    it { should_not be_valid } 
    end 

    describe "when password doesn't match confirmation" do 
    before { @user.password_confirmation = "mismatch" } 
    it { should_not be_valid } 
    end 

    describe "when email doesn't match confirmation" do 
    before { @user.email_confirmation = "[email protected]" } 
    it { should_not be_valid } 
    end 

    describe "when password confirmation is nil" do 
    before { @user.password_confirmation = nil } 
    it { should_not be_valid } 
    end 

    describe "when email confirmation is nil" do 
    before { @user.email_confirmation = nil } 
    it { should_not be_valid } 
    end 

    describe "with a password that's too short" do 
    before { @user.password = @user.password_confirmation = "a" * 5 } 
    it { should be_invalid } 
    end 

    describe "return value of authenticate method" do 
    before { @user.save } 
    let(:found_user) { User.find_by_email(@user.email) } 

    describe "with valid password" do 
     it { should == found_user.authenticate(@user.password) } 
    end 

    describe "with invalid password" do 
     let(:user_for_invalid_password) { found_user.authenticate("invalid") } 

     it { should_not == user_for_invalid_password } 
     specify { user_for_invalid_password.should be_false } 
    end 
    end 
end 

schema.rb

ActiveRecord::Schema.define(:version => 20130417021135) do 

    create_table "users", :force => true do |t| 
    t.string "first_name" 
    t.string "last_name" 
    t.string "email" 
    t.datetime "created_at",  :null => false 
    t.datetime "updated_at",  :null => false 
    t.string "password_digest" 
    end 

    add_index "users", ["email"], :name => "index_users_on_email", :unique => true 

end 
+1

このモデルをサポートするテーブルのDBスキーマを貼り付けることはできますか? –

+0

投稿を編集しました – josephndenton

+0

ありがとうございました。 Railsには、確認の検証機能が組み込まれています。下記の提案をご覧ください。 –

答えて

19

をテストemail_confirmation。されています

before { @user = User.new(email: "[email protected]", 
         first_name: "John", last_name: "Smith", 
         password: "foobar", password_confirmation: "foobar", 
         email_confirmation: "[email protected]") } 

:デフォルトでは、ActiveRecordのは、モデルを構築するために使用する属性と同じ名前のDBの列を探しますが、この行は、データベースが知らない属性を持つユーザーを構築しようとしていますあなたは本当にデータベースの電子メールの確認を保存するつもりですか、またはそれを保存する前にそれが電子メールと一致することをチェックしたいだけですか?私は後者を想定し、Railsは、実際に内蔵されただけでそれを行うためのサポート:

class User < ActiveRecord::Base 
    validates :email, :confirmation => true 
    validates :email_confirmation, :presence => true 
end 

はよりRails Guide to Validationsの詳細、またはvalidates_confirmation_of APIのドキュメントを参照してください。

+0

私も同じ問題がありました。 DBスキーマが問題でした(私はそれが移行だと思いました)。ありがとう! – Jeff

+1

移行が特定の環境でまだ行​​われていないことがあります。 'rake RAILS_ENV = production db:migrate'のような問題のコマンドは、問題を解決します。 –

+0

スキーマを変更した場合は、クッキーを削除してみることもできます –

10

私は、上記の回答が正しいとマークされ、OPの問題を解決することを理解しています。しかし、このトピックのいくつかのスタックオーバーフローの投稿では無視されないこのエラーの原因がもう1つあります。このエラーは、has_manyにas:オプションを使用するのを忘れたときに、多対多の多対多で発生する可能性があります。たとえば:

class AProfile < ActiveRecord::Base 
    has_many :profile_students 
    has_many :students, through: :profile_students 
end 

class BProfile < ActiveRecord::Base 
    has_many :profile_students 
    has_many :students, through: :profile_students 
end 

class ProfileStudent < ActiveRecord::Base 
    belongs_to :profile, polymorphic: :true 
    belongs_to :student 
end 

class Student < ActiveRecord::Base 
    has_many :profile_students 
    has_many :aprofiles, through: :profile_students 
    has_many :bprofiles, through: :profile_students 
end 

これはあなたにこのエラーを与える:

Getting “ActiveRecord::UnknownAttributeError: unknown attribute: profile_id 

次の操作を実行しようとすると:

a = AProfile.new 
a.students << Student.new 

ソリューションを追加することです:をAProfileにオプションとしておよびBProfile:

class AProfile < ActiveRecord::Base 
    has_many :profile_students, as: :profile 
    has_many :students, through: :profile_students 
end 

class BProfile < ActiveRecord::Base 
    has_many :profile_students, as: :profile 
    has_many :students, through: :profile_students 
end 
-1

私はsaを持っています私のメッセージのエラーと私は、データベース内の列定義の同じ順序としてのparamsを注文修正:

CONTROLLER

def create 
    worktime = Worktime.create(name: params[:name], workhours: params[:workhours], organization: @organization, workdays: params[:workdays]) 

    render json: worktime 
end 

DATABASE

Table: worktimes 
Columns: 
id int(11) AI PK 
name varchar(255) 
workhours text 
organization_id int(11) 
workdays text 
+5

私は99.999%の問題を抱えていますが、特定の順序でパラメータを設定することで解決できませんでした。そうであれば、何か他のものが遊んでいた。 – DickieBoy

9

はちょうど私が考えた、この私自身のインスタンスをデバッグする時間のトンを過ごしました私は3番目の可能性でチャイムインします。

私はマイグレーションを正しく実行し、レールコンソールのActiveRecordを調べて確認しました。私はスキーマから何度もデータベースを再作成しようとしていましたが、何度もマイグレーションを再実行しようとしました。

問題は、私の場合、実行時ではなく単体テストを実行しているときに問題が発生していることです。問題は、マイグレーション/ロールバックテストでテストデータベースが同期しなくなったことです。解決策は非常に簡単でした。

rake db:test:prepare 
+0

良いキャッチ。私のケースは少し違っていました。私はRails 5.0.0.1でSQLスキーマ形式を使用していました.db:rollbackとdb:migrateをもう一度実行しました。ロールバックと移行の間で、私は最後のdb移行ファイルを変更しました。 http://edgeguides.rubyonrails.org/active_record_migrations.html#changing-existing-migrationsに記載されているように、このパターンは推奨されませんが、初期の開発では便利です。どのように、なぜ、なぜ、生成されたstructure.sqlスキーマが私にとって問題であったのか分からず、それを削除してdb:migrateを再度実行しなければなりませんでした。 – bhtabor

+0

これは答えに大きな助けとなりました。広告として働いた! – jlbenc

関連する問題