【问题标题】:Factory-girl: 'association' named column overlap with Factory method named 'association'工厂女孩:名为“关联”的列与名为“关联”的工厂方法重叠
【发布时间】:2011-08-25 18:39:33
【问题描述】:

我有以下工厂定义。

Factory.define :status do |f|

end 

Factory.define :my_status , :parent => :status do |f|
  f.association 'something_here'
  f.alias 'something_here'
  f.name  'something_here'
end 

我知道工厂定义的“关联”方法,例如: f.association :group, :factory => :group 但我实际上有一个名为关联的列。将值分配给我的列的方式是什么?

更新: 在 Maletor 的帖子之后,我找到了一种解决方法 - 感谢 Maletor

我将此添加到我的状态模型中

  alias_attribute :assoc, :association 

现在我可以了

Factory.define :my_status , :parent => :status do |f|
  f.assoc 'somthing_here'
  f.alias 'somthing_here'
  f.name  'somthing_here'
end

工作正常:)

【问题讨论】:

    标签: ruby-on-rails factory-bot rspec-rails


    【解决方案1】:

    您可以在f.after_create 中分配它。虽然没有那么优雅。重命名列也可能不是一个坏主意。

    【讨论】:

    • 这是一个好方法,问题是我正在预加载那些工厂,它在我的情况下不起作用。重命名表列不是一种选择,但这带来了在模型中为此列创建别名的想法。我在上面的描述中添加了这个
    猜你喜欢
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多