Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-3983

undefined method `substitute' for polymorphic object

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 5.0.2
    • Component/s: None
    • Labels:
      None

      Description

      I'm not sure if this is an interaction problem with FactoryGirl, but I've stripped this problem down to the barebones and I can't seem to get a valid build for a polymorphic relation. Perhaps I don't have the after block right, but from everything I read, this is the proper way to satisfy the presence requirement on the relation. I get the following error when running the simple test below:

      NoMethodError: undefined method `substitute' for #<Address:0x007fd40bdb9c00>
          ~/.rvm/gems/ruby-2.1.5/gems/mongoid-4.0.2/lib/mongoid/relations/accessors.rb:231
      

      Here is the simple test:

      require 'factory_girl'
      require 'mongoid'
      require 'minitest/autorun'
       
      # Ensure backward compatibility with Minitest 4
      Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
       
      Mongoid.configure.connect_to("mongoid_test")
       
      class Event
        include Mongoid::Document  
        field :name, type: String
        has_one :address, as: :addressable
        validates :address, presence: true  
      end
       
      class User
        include Mongoid::Document  
        field :email, type: String
        has_many :addresses, as: :addressable  
      end
       
      class Address
        include Mongoid::Document
        belongs_to :addressable, polymorphic: :true
        field :city, type: String  
      end
       
      FactoryGirl.define do
        factory :event do
          name 'My Awesome Event'
          
          after(:build) do |event|
            event.address ||= FactoryGirl.build(:event_address, addressable: event)
          end
        end
      end
       
      FactoryGirl.define do
        factory :user do
          email 'me@nowhere.com'
        end
      end
       
      FactoryGirl.define do
        factory :user_address, class: 'Address' do
          association :addressable, factory: :user
          city 'Pilsen'
        end
        
        factory :event_address, class: 'Address' do
          association :addressable, factory: :event
          city 'Prague'
        end
      end
       
      class BugTest < Minitest::Test
        def test_polymorphic
          e = FactoryGirl.build(:event)
          assert e.valid?, e.errors.full_messages
        end
      end
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned
              Reporter:
              EarlAbides EarlAbides [X] (Inactive)
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: