Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用带mongoid的factory\u girl测试/references\u many中的引用\u_Ruby On Rails_Ruby On Rails 3_Mongoid_Factory Bot - Fatal编程技术网

Ruby on rails 使用带mongoid的factory\u girl测试/references\u many中的引用\u

Ruby on rails 使用带mongoid的factory\u girl测试/references\u many中的引用\u,ruby-on-rails,ruby-on-rails-3,mongoid,factory-bot,Ruby On Rails,Ruby On Rails 3,Mongoid,Factory Bot,我正在尝试测试订阅服务的关联文档。每个订阅都嵌入到一个帐户中并引用一个计划。下面是各种代码位: 账户: Factory.define :account, :class => Account do |a| a.subdomain 'test' a.agents { [ Factory.build(:user) ] } a.subscription { Factory.build(:free_subscription) } end 订阅: Factory.define :free

我正在尝试测试订阅服务的关联文档。每个订阅都嵌入到一个帐户中并引用一个计划。下面是各种代码位:

账户:

Factory.define :account, :class => Account do |a|
  a.subdomain 'test'
  a.agents { [ Factory.build(:user) ] }
  a.subscription { Factory.build(:free_subscription) }
end
订阅:

Factory.define :free_subscription, :class => Subscription do |s|
  s.started_at Time.now
  s.plan { Factory.build(:free_plan) }
end
该计划:

Factory.define :free_plan, :class => Plan do |p|
  p.plan_name 'Free'
  p.cost 0
end
错误:

Mongoid::Errors::InvalidCollection: Access to the collection for Subscription is not allowed since it is an embedded document, please access a collection from the root document.
如果我注释掉了将计划链接到订阅的行,那么测试就会起作用,但显然我无法测试订阅是否有计划

如有任何建议,将不胜感激

更新:

以下是模型:

class Account
  include Mongoid::Document

  field :company_name, :type => String
  field :subdomain, :type => String
  field :joined_at, :type => DateTime

  embeds_one :subscription

  accepts_nested_attributes_for :subscription

  before_create :set_joined_at_date

  private

  def set_joined_at_date
    self.joined_at = Time.now
  end
end

class Subscription
  include Mongoid::Document

  field :coupon_verified, :type => Boolean
  field :started_at, :type => DateTime

  referenced_in :plan
  embedded_in :account, :inverse_of => :subscription
end

class Plan
  include Mongoid::Document

  field :plan_name, :type => String
  field :cost, :type => Integer
  field :active_ticket_limit, :type => Integer
  field :agent_limit, :type => Integer
  field :company_limit, :type => Integer
  field :client_limit, :type => Integer
  field :sla_support, :type => Boolean
  field :report_support, :type => Boolean

  references_many :subscriptions
end

您需要使用订阅创建一个帐户才能使其有效

Factory.define :free_subscription, :class => Subscription do |s|
  s.started_at Time.now
  s.plan { Factory.build(:free_plan) }
  s.account { Factory(:account) }
end

您需要使用订阅创建一个帐户才能使其有效

Factory.define :free_subscription, :class => Subscription do |s|
  s.started_at Time.now
  s.plan { Factory.build(:free_plan) }
  s.account { Factory(:account) }
end

你能发布模型以便我们能准确地看到关系是如何建立的吗?你能发布模型以便我们能准确地看到关系是如何建立的吗?