我有一个 Rails 4 beta 应用程序(在 Ruby 2 上),我遇到了一个我无法理解的错误。
我有一些规范失败,因为我的模型类没有方法“create”,即使我是从 ActiveRecord::Base 继承的。错误消息将我的类称为模块(undefined method 'create' for Topic:Module
),这似乎很奇怪。
规格/模型/topic_spec.rb:
require "spec_helper"
describe Topic do
it "should create a new topic given valid attributes" do
Topic.create!({:created_by_id => 1, :title => "Test" })
end
end
应用程序/模型/topic.rb
class Topic < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
validates :title => :presence => ture
validates :created_by_id => :presence => true
end
错误信息:
$ rspec spec/models/topic_spec.rb
F
Failures:
1) Topic should create a new topic given valid attributes
Failure/Error: Topic.create!({:created_by_id => 1, :title => "Test" })
NoMethodError:
undefined method `create' for Topic:Module
# ./spec/models/topic_spec.rrc:15:in `block (2 levels) in <top (required)>'