迁移:
class CreateTableSalonWebProfiles < ActiveRecord::Migration
def change
create_table :salon_web_profiles do |t|
t.references :salon, null: false
t.boolean :attendance_domicile, default: false
t.boolean :parking_lot, default: false
t.boolean :wifi, default: false
t.boolean :physically_impaired_accessibility, default: false
t.boolean :snack_bar, default: false
t.boolean :parking_meter, default: false
t.timestamps
end
add_index :salon_web_profiles, :salon_id, unique: true
add_foreign_key :salon_web_profiles, :salons
end
end
沙龙模特:
class Salon < ActiveRecord::Base
...
has_one :salon_web_profile, dependent: :destroy
...
end
新模型:
class SalonWebProfile < ActiveRecord::Base
belongs_to :salon
validates_presence_of :salon
validates_uniqueness_of :salon
end
新的RSpec:
require 'spec_helper'
describe SalonWebProfile do
it { is_expected.to validate_presence_of(:salon) }
it { is_expected.to validate_uniqueness_of(:salon) }
end
错误:
2) SalonWebProfile should require salon to be set
Failure/Error: it { is_expected.to validate_presence_of(:salon) }
ActiveRecord::StatementInvalid:
Mysql2::Error: Unknown column 'salon_web_profiles.salon' in 'where clause': SELECT 1 AS one FROM `salon_web_profiles` WHERE `salon_web_profiles`.`salon` IS NULL LIMIT 1
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/mysql2-0.3.21/lib/mysql2/client.rb:80:in `_query'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/mysql2-0.3.21/lib/mysql2/client.rb:80:in `block in query'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/mysql2-0.3.21/lib/mysql2/client.rb:79:in `handle_interrupt'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/mysql2-0.3.21/lib/mysql2/client.rb:79:in `query'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/ar-octopus-0.8.6/lib/octopus/abstract_adapter.rb:15:in `instrument'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/validator.rb:97:in `validation_errors'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/validator.rb:77:in `collect_messages'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/validator.rb:40:in `messages'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/validator.rb:48:in `has_messages?'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:277:in `has_messages?'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:273:in `errors_match?'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:224:in `block in matches?'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:220:in `each'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:220:in `none?'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/allow_value_matcher.rb:220:in `matches?'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/disallow_value_matcher.rb:16:in `matches?'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/validation_matcher.rb:56:in `disallows_value_of'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/shoulda-matchers-2.8.0/lib/shoulda/matchers/active_model/validate_presence_of_matcher.rb:126:in `matches?'
# ./spec/models/salon_web_profile_spec.rb:4:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# Mysql2::Error:
# Unknown column 'salon_web_profiles.salon' in 'where clause'
# /home/belasis/.rvm/gems/ruby-2.3.1@user/gems/mysql2-0.3.21/lib/mysql2/client.rb:80:in `_query'
我无法理解发生了什么。在数据库中搜索时,它是否应该转换为salon_id
?
rails的版本是3.2.22.5,RSpec版本是3.7。
当我使用rails c
并尝试从沙龙创建/访问salon_web_profile时,该错误不会发生,反之亦然。
答案 0 :(得分:0)
尝试使用rails测试控制台,即rails c test,如果发生同样的错误,则尝试运行rake db:migrate RAILS_ENV = test