运行测试时遇到此错误。我已经检查过以确保所有email_confirmation
拼写正确并且(除非我疯了)他们是。我有点像Rails noob,所以它可能很简单。
用户模型
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+\-.]+@[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测试
require 'spec_helper'
describe User do
before { @user = User.new(email: "user@example.com",
first_name: "John", last_name: "Smith",
password: "foobar", password_confirmation: "foobar",
email_confirmation: "user@example.com") }
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[user@foo,com user_at_foo.org example.user@foo.
foo@bar_baz.com foo@bar+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[user@foo.COM A_US-ER@f.b.org frst.lst@foo.jp a+b@baz.cn]
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 = "mismatch@example.com" }
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
答案 0 :(得分:24)
您收到UnknownAttributeError
,因为您的users
表格中没有名为email_confirmation
的列。默认情况下,ActiveRecord将查找与用于构造模型的属性相同的DB列,但此行尝试使用数据库不知道的属性构造User:
before { @user = User.new(email: "user@example.com",
first_name: "John", last_name: "Smith",
password: "foobar", password_confirmation: "foobar",
email_confirmation: "user@example.com") }
您真的打算在数据库中保存电子邮件确认,还是只想在保存之前检查它是否与电子邮件匹配?我假设后者,Rails实际上已经内置了对此的支持:
class User < ActiveRecord::Base
validates :email, :confirmation => true
validates :email_confirmation, :presence => true
end
详细了解Rails Guide to Validations或validates_confirmation_of
API文档。 (你可能需要对:password_confirmation
做同样的事情。)
答案 1 :(得分:18)
我花了很多时间调试我自己的实例,我想我会第三种可能性。
我已经正确完成了迁移并通过检查rails控制台中的ActiveRecord
进行了验证。我曾尝试多次从模式中重新创建数据库,并尝试多次重新运行迁移,但都无济于事。
在我的情况下,问题是我在运行单元测试时遇到了问题,而不是在运行时。问题是我的测试数据库在我的迁移/回滚测试中已经不同步了。解决方案非常简单。我所要做的就是用以下方法重置测试数据库:
rake db:test:prepare
答案 2 :(得分:11)
我知道上面的答案标记正确并解决了OP的问题。但是,在此主题的许多stackoverflow帖子中都没有注意到此错误的另一个原因。当您忘记使用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中添加:as选项:
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
答案 3 :(得分:3)
我有同样的问题,这就像魔法一样。对于要更新的每个模型,在迁移语句末尾添加此行。重置有关列的所有缓存信息,这将导致它们在下一个请求时重新加载。
<ModelName>.reset_column_information
参考:https://apidock.com/rails/ActiveRecord/Base/reset_column_information/class
答案 4 :(得分:0)
当一个表没有该列时,我有很多次出现此错误,但是这次我有一个奇怪的原因。由于某种原因,我的迁移是完美的,但是(奇怪的是)schema.rb
尚未更新,因此rake db:migrate db:seed
没有创建该列。我不知道为什么会这样。
TL; DR,如果您的迁移是最新的,请检查schema.rb
并确保它也是
答案 5 :(得分:-1)
我有相同的消息错误,我修复了将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