我对rails很新。我试图通过两个表执行者和性能之间的关系创建一个简单的has_many。 我甚至无法通过基本的rspec测试。我不知道我做错了什么
这是我得到的错误
1) Performers to Performances relationship
Failure/Error: let(:relationship) { performer.performer_performances.build(
performance_id: performance.id) }
NameError:
uninitialized constant Performer::PerformerPerformance
# ./spec/models/performer_performance_spec.rb:12:in `block (2 levels) in <t
op (required)>'
# ./spec/models/performer_performance_spec.rb:14:in `block (2 levels) in <t
op (required)>'
# ./spec/models/performer_performance_spec.rb:15:in `block (2 levels) in <t
op (required)>'
这里是我的rspec文件(我现在把方法改为更简洁的测试方法,但是你可以看到我想通过但我已经评论过了)
\规格\模型\ performer_performance_spec.rb
require 'spec_helper'
describe "Performers to Performances relationship" do
let(:performer) { FactoryGirl.create(:performer) }
let(:performance) { FactoryGirl.create(:performance) }
let(:relationship) { performer.performer_performances.build(performance_id: performance.id) }
subject { relationship }
it { should be_valid }
#before do
# @example_performer = FactoryGirl.create(:performer)
# @example_performance = FactoryGirl.create(:performance)
#end
it "should recognise when a performer has no performances" do
# @example_performer.performances.count.should == 0
end
it "should handle a performer with a performance" do
# @example_performer.performances << @example_performance
# @example_performer.performances.count.should == 1
end
it "should automatically know a performance's performer" do
# @example_performer.performances << @example_performance
# @example_performance.performers.count.should == 1
end
it "should not allow the same artist twice in a performance" do
# @example_performance.performers << @example_performer
# @example_performance.performers << @example_performer
# @example_performance.performers.count.should == 1
end
end
这是我的文件
\应用\模型\ performance.rb
class Performance < ActiveRecord::Base
validates :file_name, presence: true, length: { maximum: 50 }, uniqueness: { case_sensitive: false }
validates :date, length: { maximum: 30 }
validates :location, length: { maximum: 50 }
has_many :performer_performances, dependent: :destroy
has_many :performers, through: :performer_performances
end
\应用\模型\ performer.rb
class Performer < ActiveRecord::Base
validates :name, presence: true, length: { maximum: 50 }, uniqueness: { case_sensitive: false }
has_many :performer_performances, dependent: :destroy
has_many :preformances, through: :performer_performances
end
\分贝\迁移\ 20140315155732_create_performer_performance.rb
class CreatePerformerPerformance < ActiveRecord::Migration
def change
create_table :performer_performances do |t|
t.belongs_to :performer
t.belongs_to :performance
t.timestamps
end
end
add_index :performer_performances, :performer_id
add_index :performer_performances, :performance_id
add_index :performer_performances, [:performer_id, :performance_id], unique: true
end
\规格\ factories.rb
FactoryGirl.define do
sequence :name do |n|
"slimshady#{n}"
end
factory :performer do
name :name
end
factory :performance do
file_name :name
date Date.new(2008, 12, 22)
location "lame house"
end
end
我运行bundle exec rake db:migrate
和test:prepare
以防我的Gemfile
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.3'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '3.1.2'
gem 'faker', '1.1.2' #ch9.29
gem 'will_paginate', '3.0.4' #ch9.3
gem 'bootstrap-will_paginate', '0.0.9'
group :development :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
gem 'factory_girl_rails', '4.2.1'
end
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
我没有找到任何拼写错误,所以我更新了这个。 我正在运行windows vista x64,如果这改变了什么......
答案 0 :(得分:1)
由于您没有显示该文件,我假设您尚未在performer_performance.rb
中创建app/models
文件。迁移负责创建数据库表,但您仍需要通过使用适当的类定义创建主题文件来创建ActiveRecord模型。
您得到的错误是因为Ruby / Rails遇到常量引用(即PerformerPerformance
)并且找不到具有相应定义的相应文件。