Git Product home page Git Product logo

Comments (3)

joshuaclayton avatar joshuaclayton commented on July 17, 2024

@cscairns Sorry to hear the upgrade broke things; could you post your factories somewhere so I could take a peek? I'm wondering if there was a prior bug with attribute ordering that you were somehow depending on that got fixed and broke things.

I'd also check that the record doesn't consider itself baseball when you set the id/type to nil (and figure out why it is!)

from factory_bot_rails.

cscairns avatar cscairns commented on July 17, 2024

Sure, here:

factory :matchup do
   matchupable { FactoryGirl::Singleton.execute(:baseball_matchup) }

    trait :non_baseball_matchup do
      matchupable_id nil
      matchupable_type nil
    end
  end

  factory :baseball_matchup do
    after(:build) do |bm|
      bm.matchup = FactoryGirl::Singleton.execute(:matchup, :matchupable => bm)
    end
  end

If you're wondering what FactoryGirl::Singleton.execute is, please, see: http://stackoverflow.com/questions/9678365/factorygirl-in-rails-associations-w-unique-constraints.

from factory_bot_rails.

mike-burns avatar mike-burns commented on July 17, 2024

Hi @cscairns . I am unable to reproduce this.

I made a Rails app with these models:

class Matchup < ActiveRecord::Base
  belongs_to :matchupable, polymorphic: true
  validates :matchupable_id, :matchupable_type, presence: true,
    if: Proc.new { |record| record.baseball? }
end

class BaseballMatchup < ActiveRecord::Base
  has_one :matchup, as: :matchupable
end

Defined with this migration:

class CreateTables < ActiveRecord::Migration
  def up
    create_table :matchups do |t|
      t.timestamps
      t.integer :matchupable_id
      t.string :matchupable_type
      t.boolean :baseball, null: false, default: false
    end

    create_table :baseball_matchups do |t|
      t.timestamps
    end
  end

  def down
    drop_table :baseball_matchups

    drop_table :matchups
  end
end

And this spec:

require 'spec_helper'

describe Matchup do
  it 'is valid without a matchupable' do
    matchup = FactoryGirl.build(:matchup, matchupable_id: nil, matchupable_type: nil)
    matchup.should be_valid
  end
end

With this in spec/factories.rb:

module FactoryGirl
  class Singleton
    @@singletons = {}

    def self.execute(factory_key)
      begin
        @@singletons[factory_key] = FactoryGirl.create(factory_key)
      rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique
        # already in DB so return nil
      end

      @@singletons[factory_key]
    end
  end
end

FactoryGirl.define do
  factory :matchup do
    matchupable { FactoryGirl::Singleton.execute(:baseball_matchup) }

    trait :non_baseball_matchup do
      matchupable_id nil
      matchupable_type nil
    end
  end

  factory :baseball_matchup do
    after(:build) do |bm|
      bm.matchup = FactoryGirl::Singleton.execute(:matchup, :matchupable => bm)
    end
  end
end

This is the result:

% ./.binstubs/rspec spec/models/matchup_spec.rb
.

Finished in 0.07615 seconds
1 example, 0 failures

Please comment if you can narrow the issue further.

Thanks,
-Mike

from factory_bot_rails.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.