2013-05-21 42 views
0

我有一個Rails 4測試版應用程序(在Ruby 2上),並且出現了一個我無法理解的錯誤。'創建'的ActiveRecord模型子類NoMethodError

我有一些規格失敗,因爲我的模型類沒有創建方法,即使我從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)>' 

回答

1

這聽起來像你有一個模塊或命名空間也被命名爲主題,首先得到加載,所以在你的測試中,主題不是指你的類。是否有任何其他文件中有主題,甚至像課程主題::問題或類似?如果是這樣,請嘗試將它們取出或明確表示。例如,更改:

class Topic::Question < ActiveRecord::Base 

class Topic 
    class Question < ActiveRecord::Base 
+0

正確的金錢。該應用程序被稱爲「主題」。非常感謝! –