在这个问题上,我已经用头撞了一段时间了,我就是不能让它工作。我有三个模型:
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每当我尝试出厂(:instrument)或出厂(:analytical_method)时,它抛出以下错误:
NoMethodError:
undefined method `analyte=' for #<AnalyticalMethod:0x00000104c44758>我是不是漏掉了什么可笑的打字错误?这个网站运行得很好,但测试总是失败。谢谢你帮我恢复理智!
发布于 2011-04-30 10:31:51
我相信这是因为您正在使用instrument.association :analyte和analytical_method.association :analyte建立has_many关系。关联声明用于belongs_to关系。
我通常不会使用Factory Girl来创建has_many关系,但是如果您选择了这种方式,您并不是第一个这样做的人。Here's a blog post,它有几年历史,但似乎描述了您正在尝试做的事情。
https://stackoverflow.com/questions/5839016
复制相似问题