Search code examples
ruby-on-rails-3mongoid3fabrication-gem

get mongoid <-> fabrication not working habtm


I have a question to the "has_and_belongs_to_many" and "accept_nested_attributes_for" in context to fabrication and mongoid

I have a Location which can have many services

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

on my Fabrication Files I have this

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

In this case my rspec hang up.

Can someone provide a working example with *accept_nested_attributes* and / or *has_and_belongs_to_many* with mongoid and the fabrication gem (which works "out of the box" with mongoid?

any suggestions?

I am working with mongoid3


Solution

  • This all depends on how you are using Fabrication.

    Nesting parameters for testing controllers is a world of hurt and you will need some hand crafting for your params hash.

    For models you should try this:

    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
    

    You probably don't need the :inverse_of for your relations.