【问题标题】:get mongoid <-> fabrication not working habtm得到 mongoid <-> 制造不工作 habtm
【发布时间】:2012-11-15 13:16:07
【问题描述】:

我有一个问题 “has_and_belongs_to_many”和“accept_nested_attributes_for” 在制造和mongoid的上下文中

我有一个可以提供许多服务的位置

class Location
  include Mongoid::Document

  field :name
  field :service

  has_and_belongs_to_many :services, inverse_of: :locations, autosave: true, dependent: :delete
  accepts_nested_attributes_for :services

  attr_accessible :services, :name



class Service
  include Mongoid::Document

  field :name, type: String

  has_and_belongs_to_many :locations, inverse_of: :services, autosave: true
  accepts_nested_attributes_for :locations
  attr_accessible :name, :icon, :description

在我的制造文件中我有这个

Fabricator(:service) do
  initialize_with { Location.produce(:location) }
  name "Service Name"
  description "Lorem ipsum est lauda en radios"
  location
end

Fabricator(:location) do
  name "Special Club"
  service
end

在这种情况下,我的 rspec 挂断了。

有人可以提供一个带有 *accept_nested_attributes* 和/或 *has_and_belongs_to_many* 的工作示例以及 mongoid 和制造 gem(它适用于 mongoid “开箱即用”吗?

有什么建议吗?

我正在使用 mongoid3

【问题讨论】:

    标签: ruby-on-rails-3 mongoid3 fabrication-gem


    【解决方案1】:

    这一切都取决于您如何使用 Fabrication。

    测试控制器的嵌套参数是一个痛苦的世界,您需要手工制作参数哈希。

    对于模型,你应该试试这个:

    Fabricator(:service) do
      name "Service Name"
      description "Lorem ipsum est lauda en radios"
      locations {[Fabricate.build(:location)]}
    end
    
    Fabricator(:location) do
      name "Special Club"
      service
    end
    

    您的关系可能不需要:inverse_of

    【讨论】:

    • 谢谢Tsagadai,周末后试一试!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多