首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails 3、Rspec/Cucumber和Factory Girl:嵌套/关联对象上没有方法错误

Rails 3、Rspec/Cucumber和Factory Girl:嵌套/关联对象上没有方法错误
EN

Stack Overflow用户
提问于 2011-04-30 10:17:17
回答 1查看 1.1K关注 0票数 1

在这个问题上,我已经用头撞了一段时间了,我就是不能让它工作。我有三个模型:

代码语言:javascript
复制
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

我有以下的工厂:

代码语言:javascript
复制
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)时,它抛出以下错误:

代码语言:javascript
复制
NoMethodError:  
  undefined method `analyte=' for #<AnalyticalMethod:0x00000104c44758>

我是不是漏掉了什么可笑的打字错误?这个网站运行得很好,但测试总是失败。谢谢你帮我恢复理智!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-30 10:31:51

我相信这是因为您正在使用instrument.association :analyteanalytical_method.association :analyte建立has_many关系。关联声明用于belongs_to关系。

我通常不会使用Factory Girl来创建has_many关系,但是如果您选择了这种方式,您并不是第一个这样做的人。Here's a blog post,它有几年历史,但似乎描述了您正在尝试做的事情。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5839016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档