2011-04-30 47 views
1

我一直在這一個抨擊我的頭靠在牆上,我無法讓它工作。我有三個型號:Rails 3,Rspec /黃瓜和工廠女孩:嵌套/關聯對象沒有方法錯誤

class Instrument < ActiveRecord::Base 
    has_many :analytical_methods 
    has_many :analytes, :through => :analytical_methods 
    accepts_nested_attributes_for :analytical_methods 
    attr_accessible :name, :analytical_methods_attributes 
end 

class AnalyticalMethod < ActiveRecord::Base 
    belongs_to :instrument 
    has_many :analytes 
    accepts_nested_attributes_for :analytes 
    attr_accessible :name, :analytes_attributes 
end 

class Analyte < ActiveRecord::Base 
    belongs_to :analytical_method 
    attr_accessible :name 
end 

而且我有以下工廠:

Factory.define :analyte do |analyte| 
    analyte.name "Test analyte" 
end 

Factory.define :analytical_method do |analytical_method| 
    analytical_method.name "Test method" 
    analytical_method.association :analyte 
end 

Factory.define :instrument do |instrument| 
    instrument.name "Test instrument" 
    instrument.association :analytical_method 
    instrument.association :analyte 
end 

任何時候,我嘗試廠(:儀器)或廠(:analytical_method),它引發以下錯誤:

NoMethodError: 
    undefined method `analyte=' for #<AnalyticalMethod:0x00000104c44758> 

我錯過了一些荒謬的錯字或什麼?該網站工作得很好,但測試保持失敗。感謝任何幫助恢復我的理智!

回答

1

我相信這是因爲您正在使用instrument.association :analyteanalytical_method.association :analyte作爲has_many關係。 關聯聲明用於belongs_to關係。

我通常不使用Factory Girl創建has_many關係,但如果您選擇走這條路線,那麼您不是第一個這樣做的人。 Here's a blog post這已經有幾年了,但似乎描述了你想要做的事情。