Comments (3)
@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.
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.
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)
- Reloading Issue Triggering Resolved Rails Bug HOT 16
- [Feature Request] Install Generator HOT 4
- Issues with custom error message translations HOT 21
- incompatible marshal file format (can't be read) HOT 4
- Factory not registered: "credit_card" HOT 13
- 5.0.x does not play well with Spring HOT 5
- factory_bot_rails 6.0 checklist HOT 2
- Invalid DateTime.now field on factory creation
- Multiple Tests Fail on Vanilla `bundle exec rake` HOT 2
- Model generation ignores --force-plural HOT 3
- Rails ignoring factory HOT 2
- Install fewer gems on Travis HOT 1
- Incompatibility with Rails 6.0.3? HOT 2
- Perform DB migration will make factory_bot looking for methods in the wrong place HOT 2
- Successive associations issue when migrating to rails 6.1 HOT 3
- Switch from Travis CI to GitHub Actions
- Duplicate factories problem HOT 5
- Automatically populate attributes and types when generating factories for existing models HOT 2
- Cannot create Factory with multiple traits got (ArgumentError Exception: wrong number of arguments (given 1, expected 0)) Ruby 3.0, Rails 6.1.72, factory_bot_rails 6.1.0 HOT 2
- Dead link in CONTRIBUTING.md HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from factory_bot_rails.