Ruby on rails Rails 4-获取ActiveRecord::UnknownAttributeError:未知属性:checker\u id
初学者Rails程序员在这里。 运行rspec测试时,我遇到以下错误:Ruby on rails Rails 4-获取ActiveRecord::UnknownAttributeError:未知属性:checker\u id,ruby-on-rails,ruby,activerecord,rspec,railstutorial.org,Ruby On Rails,Ruby,Activerecord,Rspec,Railstutorial.org,初学者Rails程序员在这里。 运行rspec测试时,我遇到以下错误: 1) User checking Failure/Error: @user.check!(other_user) ActiveRecord::UnknownAttributeError: unknown attribute: checker_id # ./app/models/user.rb:33:in `check!' # ./spec/models/user_spe
1) User checking
Failure/Error: @user.check!(other_user)
ActiveRecord::UnknownAttributeError:
unknown attribute: checker_id
# ./app/models/user.rb:33:in `check!'
# ./spec/models/user_spec.rb:87:in `block (3 levels) in <top (required)>'
2) User checking checked user checkers
Failure/Error: @user.check!(other_user)
ActiveRecord::UnknownAttributeError:
unknown attribute: checker_id
# ./app/models/user.rb:33:in `check!'
# ./spec/models/user_spec.rb:87:in `block (3 levels) in <top (required)>'
3) User checking checked_users
Failure/Error: @user.check!(other_user)
ActiveRecord::UnknownAttributeError:
unknown attribute: checker_id
# ./app/models/user.rb:33:in `check!'
# ./spec/models/user_spec.rb:87:in `block (3 levels) in <top (required)>'
4) User checking and unchecking
Failure/Error: @user.check!(other_user)
ActiveRecord::UnknownAttributeError:
unknown attribute: checker_id
# ./app/models/user.rb:33:in `check!'
# ./spec/models/user_spec.rb:87:in `block (3 levels) in <top (required)>'
5) User checking and unchecking checked_users
Failure/Error: @user.check!(other_user)
ActiveRecord::UnknownAttributeError:
unknown attribute: checker_id
# ./app/models/user.rb:33:in `check!'
# ./spec/models/user_spec.rb:87:in `block (3 levels) in <top (required)>'
6) Relationship
Failure/Error: let(:relationship) { checker.relationships.build(checked_id: checked.id) }
ActiveRecord::UnknownAttributeError:
unknown attribute: checker_id
# ./spec/models/relationship_spec.rb:7:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:9:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:11:in `block (2 levels) in <top (required)>'
7) Relationship when checked id is not present
Failure/Error: let(:relationship) { checker.relationships.build(checked_id: checked.id) }
ActiveRecord::UnknownAttributeError:
unknown attribute: checker_id
# ./spec/models/relationship_spec.rb:7:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:21:in `block (3 levels) in <top (required)>'
8) Relationship checker methods
Failure/Error: let(:relationship) { checker.relationships.build(checked_id: checked.id) }
ActiveRecord::UnknownAttributeError:
unknown attribute: checker_id
# ./spec/models/relationship_spec.rb:7:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:9:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:15:in `block (3 levels) in <top (required)>'
9) Relationship checker methods
Failure/Error: let(:relationship) { checker.relationships.build(checked_id: checked.id) }
ActiveRecord::UnknownAttributeError:
unknown attribute: checker_id
# ./spec/models/relationship_spec.rb:7:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:9:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:14:in `block (3 levels) in <top (required)>'
10) Relationship checker methods checked
Failure/Error: let(:relationship) { checker.relationships.build(checked_id: checked.id) }
ActiveRecord::UnknownAttributeError:
unknown attribute: checker_id
# ./spec/models/relationship_spec.rb:7:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:9:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:17:in `block (3 levels) in <top (required)>'
11) Relationship checker methods checker
Failure/Error: let(:relationship) { checker.relationships.build(checked_id: checked.id) }
ActiveRecord::UnknownAttributeError:
unknown attribute: checker_id
# ./spec/models/relationship_spec.rb:7:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:9:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:16:in `block (3 levels) in <top (required)>'
12) Relationship when checker id is not present
Failure/Error: let(:relationship) { checker.relationships.build(checked_id: checked.id) }
ActiveRecord::UnknownAttributeError:
unknown attribute: checker_id
# ./spec/models/relationship_spec.rb:7:in `block (2 levels) in <top (required)>'
# ./spec/models/relationship_spec.rb:26:in `block (3 levels) in <top (required)>'
用户_spec.rb
relationship.rb
201408183201_创建_关系.rb
我非常感谢任何能让我在这方面有所进展的想法!谢谢
编辑/升级:在执行rakedb:reset->rakedb:migrate->raketest:prepare时,我在每个步骤都检查了开发数据库。在rake db:migrate之后,users表中填充了示例用户,但是rake test:prepare删除了所有这些用户,使表为空。
也许这里有联系。因为rake test:prepare删除了它,checker_idb可能不可用吗?您发布的错误,
未知属性:checker\u id
指代码第33行中不存在的属性checker_id:
relationships.create!(checked_id: other_user.id)
因此,要么您发布了错误的代码,要么您需要重新启动测试服务器,要么存在其他问题。但基本上,您的测试套件并没有测试您认为它正在测试的内容。我最终删除了自第10章开始以来所做的所有文件和更改,然后复制粘贴了教程中的所有更改。我不知道我到底做错了什么,但这解决了问题。您是否也在测试模式下运行迁移?bundle exec rake db:migrate RAILS\u ENV=testYes。这并没有解决问题
require 'spec_helper'
describe User do
before do
@user = User.new(name: "Test User", email: "test@example.com",
verified:true, about_text:"About Text",
success_text:"Success Text", password: "foobar",
password_confirmation: "foobar")
end
subject { @user }
it { should respond_to(:name) }
it { should respond_to(:email) }
it { should respond_to(:verified) }
it { should respond_to(:about_text) }
it { should respond_to(:success_text) }
it { should respond_to(:password_digest) }
it { should respond_to(:password_confirmation) }
it { should respond_to(:remember_token) }
it { should respond_to(:authenticate) }
it { should respond_to(:admin) }
it { should respond_to(:relationships) }
it { should respond_to(:checked_users) }
it { should respond_to(:checking?) }
it { should respond_to(:check!) }
it { should respond_to(:reverse_relationships) }
it { should respond_to(:checkers) }
it { should be_valid }
it { should_not be_admin }
describe "with admin attribute set to 'true'" do
before do
@user.save!
@user.toggle!(:admin)
end
it { should be_admin }
end
describe "when name is not present" do
before { @user.name = " " }
it { should_not be_valid }
end
describe "whenn password is not present" do
before do
@user = User.new(name: "Example User", email: "example@example.com",
password: " ", password_confirmation: " ")
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 "with a password that is 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 eq found_user.authenticate(@user.password) }
end
describe "with invalid password" do
let(:user_for_invalid_password) {
found_user.authenticate("invalid") }
it { should_not eq user_for_invalid_password }
specify { expect(user_for_invalid_password).to be_false }
end
end
describe "checking" do
let(:other_user) { FactoryGirl.create(:user) }
before do
@user.save
@user.check!(other_user)
end
it { should be_checking(other_user) }
its(:checked_users) { should include(other_user) }
describe "checked user" do
subject { other_user }
its(:checkers) { should include(@user) }
end
describe "and unchecking" do
before { @user.uncheck!(other_user) }
it { should_not be_checking(other_user) }
its(:checked_users) { should_not include(other_user) }
end
end
end
class Relationship < ActiveRecord::Base
belongs_to :checker, class_name: "User"
belongs_to :checked, class_name: "User"
validates :checker_id, presence: true
validates :checked_id, presence: true
end
require 'spec_helper'
describe Relationship do
let(:checker) { FactoryGirl.create(:user) }
let(:checked) { FactoryGirl.create(:user) }
let(:relationship) { checker.relationships.build(checked_id: checked.id) }
subject { relationship }
it { should be_valid }
describe "checker methods" do
it { should respond_to(:checker) }
it { should respond_to(:checked) }
its(:checker) { should eq checker }
its(:checked) { should eq checked }
end
describe "when checked id is not present" do
before { relationship.checked_id = nil }
it { should_not be_valid }
end
describe "when checker id is not present" do
before { relationship.checker_id = nil }
it { should_not be_valid }
end
end
class CreateRelationships < ActiveRecord::Migration
def change
create_table :relationships do |t|
t.integer :checker_id
t.integer :checked_id
t.timestamps
end
add_index :relationships, :checker_id
add_index :relationships, :checked_id
add_index :relationships, [:checker_id, :checked_id], unique: true
end
end
relationships.create!(checked_id: other_user.id)